Skip to content

Commit ccbf0d9

Browse files
committed
tests: Replace sprintf with snprintf for security
Replace sprintf with snprintf in test files to prevent buffer overflows. This follows the security hardening mentioned in NEWS.md where sprintf was replaced with safer alternatives. Changes made: - tests/init.c: Replace sprintf with snprintf for proc paths - tests/tests_libcrun_fuzzer.c: Replace sprintf with snprintf for ID generation Signed-off-by: Osama Abdelkader <[email protected]>
1 parent fc084fd commit ccbf0d9

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

tests/init.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,12 @@ check_idmapped_mounts (const char *path)
200200
_exit (EXIT_SUCCESS);
201201
}
202202

203-
sprintf (proc_path, "/proc/%d/uid_map", pid);
203+
snprintf (proc_path, sizeof (proc_path), "/proc/%d/uid_map", pid);
204204
write_to (proc_path, "0 0 1");
205-
sprintf (proc_path, "/proc/%d/gid_map", pid);
205+
snprintf (proc_path, sizeof (proc_path), "/proc/%d/gid_map", pid);
206206
write_to (proc_path, "0 0 1");
207207

208-
sprintf (proc_path, "/proc/%d/ns/user", pid);
208+
snprintf (proc_path, sizeof (proc_path), "/proc/%d/ns/user", pid);
209209
fd = open (proc_path, O_RDONLY);
210210
if (fd < 0)
211211
error (EXIT_FAILURE, errno, "open `%s`", proc_path);
@@ -766,12 +766,12 @@ main (int argc, char **argv)
766766
if (argc < 3)
767767
error (EXIT_FAILURE, 0, "'create-sub-cgroup-and-wait' requires an argument");
768768

769-
sprintf (path, "/sys/fs/cgroup/%s", argv[2]);
769+
snprintf (path, sizeof (path), "/sys/fs/cgroup/%s", argv[2]);
770770
ret = mkdir (path, 0700);
771771
if (ret < 0)
772772
error (EXIT_FAILURE, errno, "mkdir");
773773

774-
sprintf (path, "/sys/fs/cgroup/%s/cgroup.procs", argv[2]);
774+
snprintf (path, sizeof (path), "/sys/fs/cgroup/%s/cgroup.procs", argv[2]);
775775

776776
fd = open (path, O_WRONLY);
777777
if (fd < 0)

tests/tests_libcrun_fuzzer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ run_one_container (uint8_t *buf, size_t len, bool detach)
336336
}
337337

338338
memset (&ctx, 0, sizeof (ctx));
339-
sprintf (id, "fuzzer-%d-%llu", getpid (), counter++);
339+
snprintf (id, sizeof (id), "fuzzer-%d-%llu", getpid (), counter++);
340340
ctx.id = id;
341341
ctx.bundle = "rootfs";
342342
ctx.detach = detach;

0 commit comments

Comments
 (0)