Harden create command and directory walk

- create: write the snapshot to a temporary file and copy it into place
  only after the walk succeeds; every failure now propagates through rc
  instead of being reported as success, and the temporary is unlinked on
  all exit paths
- create: skip snapshot files by matching the ".fsnap" suffix against the
  basename, so entries below a directory whose name contains ".fsnap" are
  no longer dropped from the snapshot
- walk: emit a record for directories as well as leaf files, honour the
  return value of the operation callback, and free the path and close the
  directory handle on every exit path
- utility: replace the fixed-size realpath buffer with realpath(NULL) and
  add copy(), which reports read, write and close errors instead of
  silently truncating the destination
- scan, create: set errno to EINVAL on invalid arguments so main can print
  a meaningful message

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Mbj1ZPUEhxqMYKsK7Y7jkt
This commit is contained in:
Fabio Scotto di Santolo
2026-08-01 18:08:26 +02:00
parent 3eae528f54
commit 777f14a9e9
6 changed files with 148 additions and 55 deletions

View File

@@ -1,17 +1,10 @@
#ifndef UTILITY_H #ifndef UTILITY_H
#define UTILITY_H #define UTILITY_H
#include <unistd.h>
#include <limits.h>
#ifdef PATH_MAX
static long pathmax = PATH_MAX;
#else
static long pathmax = 1024;
#endif
char *strconcat(const char *src, int n, ...); char *strconcat(const char *src, int n, ...);
char *resolve_path(const char *path); char *resolve_path(const char *path);
int copy(const char *src, const char *dst);
#endif #endif

View File

@@ -1,12 +1,16 @@
#define _XOPEN_SOURCE 500 #define _DEFAULT_SOURCE
#define _XOPEN_SOURCE 700
#include "create.h" #include "create.h"
#include "utility.h" #include "utility.h"
#include "walk.h" #include "walk.h"
#include <fcntl.h> #include <fcntl.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
static char file_type(mode_t mode) { static char file_type(mode_t mode) {
if (S_ISREG(mode)) if (S_ISREG(mode))
@@ -28,8 +32,14 @@ static char file_type(mode_t mode) {
} }
static int write_record(const char *path, void *context) { static int write_record(const char *path, void *context) {
FILE *snapshot = (FILE *)context; // skip snapshot files
const char *base = strrchr(path, '/');
base = base ? base + 1 : path;
size_t n = strlen(base);
if (n >= 6 && strcasecmp(base + n - 6, ".fsnap") == 0)
return 0;
FILE *snapshot = (FILE *)context;
struct stat st; struct stat st;
if (lstat(path, &st) == -1) { if (lstat(path, &st) == -1) {
return -1; return -1;
@@ -51,27 +61,53 @@ static int write_record(const char *path, void *context) {
int create(const char *directory, const char *output_file) { int create(const char *directory, const char *output_file) {
if (directory == NULL || strlen(directory) == 0) { if (directory == NULL || strlen(directory) == 0) {
errno = EINVAL;
return -1; return -1;
} }
if (output_file == NULL || strlen(output_file) == 0) { if (output_file == NULL || strlen(output_file) == 0) {
errno = EINVAL;
return -1; return -1;
} }
const char *resolved = resolve_path(directory); char *resolved = resolve_path(directory);
if (resolved == NULL) { if (resolved == NULL) {
return -1; return -1;
} }
FILE *f = fopen(output_file, "w+");
if (f == NULL) { int rc = 0;
char template[] = "/tmp/snapshot_XXXXXX";
int fd = mkstemp(template);
if (fd == -1) {
free(resolved);
return -1; return -1;
} }
if (walk(resolved, write_record, (void *)f) == -1) { FILE *tmp = fdopen(fd, "w+");
return -1; if (tmp == NULL) {
rc = -1;
goto out;
} }
if (fclose(f) != 0) { if (walk(resolved, write_record, (void *)tmp) == -1) {
return -1; rc = -1;
goto out;
} }
return 0;
if (fflush(tmp) == EOF) {
rc = -1;
goto out;
}
if (copy(template, output_file) == -1) {
rc = -1;
goto out;
}
out:
free(resolved);
unlink(template);
if (tmp != NULL && fclose(tmp) != 0) {
rc = -1;
}
return rc;
} }

View File

@@ -1,5 +1,5 @@
#include "scan.h"
#include "create.h" #include "create.h"
#include "scan.h"
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -15,6 +15,11 @@ int main(int argc, char *argv[]) {
errno = 0; errno = 0;
const char *cmd = argv[1]; const char *cmd = argv[1];
if (strcmp(cmd, "scan") == 0) { if (strcmp(cmd, "scan") == 0) {
if (argc < 3) {
fprintf(stderr, "%s: command %s expected param [directory]\n", argv[0],
cmd);
return EXIT_FAILURE;
}
const char *directory = argv[2]; const char *directory = argv[2];
if (scan(directory) == -1) { if (scan(directory) == -1) {
const int err = errno; const int err = errno;
@@ -23,15 +28,17 @@ int main(int argc, char *argv[]) {
} }
} else if (strcmp(cmd, "create") == 0) { } else if (strcmp(cmd, "create") == 0) {
if (argc < 4) { if (argc < 4) {
fprintf(stderr, "%s: command %s expected params [directory output_file]\n", argv[0], cmd); fprintf(stderr,
return EXIT_FAILURE; "%s: command %s expected params [directory output_file]\n",
argv[0], cmd);
return EXIT_FAILURE;
} }
const char *directory = argv[2]; const char *directory = argv[2];
const char *output_file = argv[3]; const char *output_file = argv[3];
if (create(directory, output_file) == -1) { if (create(directory, output_file) == -1) {
const int err = errno; const int err = errno;
fprintf(stderr, "create failed: %s\n", strerror(err)); fprintf(stderr, "create failed: %s\n", strerror(err));
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} else { } else {
fprintf(stderr, "command %s is not valid\n", cmd); fprintf(stderr, "command %s is not valid\n", cmd);

View File

@@ -1,7 +1,10 @@
#define _DEFAULT_SOURCE
#include "scan.h" #include "scan.h"
#include "utility.h" #include "utility.h"
#include "walk.h" #include "walk.h"
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
static int print_file(const char *file, void *context) { static int print_file(const char *file, void *context) {
@@ -12,11 +15,19 @@ static int print_file(const char *file, void *context) {
/* Scan recursively directory, printing all entry */ /* Scan recursively directory, printing all entry */
int scan(const char *directory) { int scan(const char *directory) {
if (directory == NULL || strlen(directory) == 0) if (directory == NULL || strlen(directory) == 0) {
errno = EINVAL;
return -1; return -1;
}
const char *dir = resolve_path(directory); char *dir = resolve_path(directory);
if (dir == NULL) if (dir == NULL)
return -1; return -1;
return walk(dir, print_file, NULL);
if (walk(dir, print_file, NULL) == -1) {
free(dir);
return -1;
}
free(dir);
return 0;
} }

View File

@@ -1,9 +1,10 @@
#define _XOPEN_SOURCE 500 #define _XOPEN_SOURCE 700
#include "utility.h" #include "utility.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <unistd.h>
char *strconcat(const char *src, int n, ...) { char *strconcat(const char *src, int n, ...) {
va_list ap; va_list ap;
@@ -37,11 +38,46 @@ char *strconcat(const char *src, int n, ...) {
} }
char *resolve_path(const char *path) { char *resolve_path(const char *path) {
char resolved[pathmax];
char *dir = NULL; char *dir = NULL;
if ((dir = realpath(path, resolved)) == NULL) { if ((dir = realpath(path, NULL)) == NULL) {
perror("realpath");
return NULL; return NULL;
} }
return dir; return dir;
} }
int copy(const char *src, const char *dst) {
FILE *file1 = fopen(src, "r");
if (file1 == NULL) {
return -1;
}
FILE *file2 = fopen(dst, "w+");
if (file2 == NULL) {
fclose(file1);
return -1;
}
char buffer[8192]; // Buffer di 8KB
size_t bytes;
while ((bytes = fread(buffer, 1, sizeof(buffer), file1)) > 0) {
if (fwrite(buffer, 1, bytes, file2) != bytes) {
fclose(file1);
fclose(file2);
return -1;
}
}
if (ferror(file1)) {
fclose(file1);
fclose(file2);
return -1;
}
if (fclose(file1) != 0)
return -1;
if (fclose(file2) != 0)
return -1;
return 0;
}

View File

@@ -1,7 +1,6 @@
#define _DEFAULT_SOURCE
#define _GNU_SOURCE #define _GNU_SOURCE
#include "walk.h"
#include "utility.h" #include "utility.h"
#include "walk.h"
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
@@ -12,7 +11,7 @@ static char *blacklist[] = {".", ".."};
static int ignore_file(const char *name) { static int ignore_file(const char *name) {
for (long unsigned int i = 0; i < sizeof(blacklist) / sizeof(char *); i++) { for (long unsigned int i = 0; i < sizeof(blacklist) / sizeof(char *); i++) {
if (strcmp(name, blacklist[i]) == 0 || strcasestr(name, ".fsnap")) if (strcmp(name, blacklist[i]) == 0)
return 1; return 1;
} }
return 0; return 0;
@@ -26,10 +25,13 @@ static int handle_unknown(const char *path, operation op, void *context) {
} }
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
if (op(path, context) == -1)
return -1;
return walk(path, op, context); return walk(path, op, context);
} }
op(path, context); if (op(path, context) == -1)
return -1;
return 0; return 0;
} }
@@ -39,42 +41,50 @@ int walk(const char *directory, operation op, void *context) {
return -1; return -1;
} }
errno = 0; int rc = -1;
char *curr = NULL; char *path = NULL;
struct dirent *direntp = NULL; struct dirent *direntp = NULL;
errno = 0;
while ((direntp = readdir(dirp)) != NULL) { while ((direntp = readdir(dirp)) != NULL) {
curr = direntp->d_name; if (ignore_file(direntp->d_name))
if (ignore_file(curr))
continue; continue;
char *path = strconcat(directory, 2, "/", curr); path = strconcat(directory, 2, "/", direntp->d_name);
if (path == NULL) if (path == NULL)
return -1; goto out;
switch (direntp->d_type) { switch (direntp->d_type) {
case DT_UNKNOWN: case DT_UNKNOWN:
// operating system or filesystem do not support dirent->d_type. // operating system or filesystem do not support dirent->d_type.
// In this case use alternative algorithm for walk directories. // In this case use alternative algorithm for walk directories.
if (handle_unknown(path, op, context) == -1) { if (handle_unknown(path, op, context) == -1)
return -1; goto out;
}
break; break;
case DT_DIR: case DT_DIR:
if (walk(path, op, context) == -1) { if (op(path, context) == -1)
return -1; goto out;
} if (walk(path, op, context) == -1)
goto out;
break; break;
default: default:
op(path, context); if (op(path, context) == -1)
goto out;
break; break;
} }
if (path != NULL) free(path);
free(path); path = NULL;
errno = 0;
} }
if (errno != 0) { if (errno == 0) {
return -1; rc = 0;
} }
return closedir(dirp);
out:
free(path);
if (closedir(dirp) == -1)
rc = -1;
return rc;
} }