Skip to content

Commit ce7cda4

Browse files
BufferOverflow Hardening
1 parent 15f855f commit ce7cda4

File tree

6 files changed

+24
-24
lines changed

6 files changed

+24
-24
lines changed

src/libcrun/chroot_realpath.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
#define MAX_READLINKS 32
4949

50-
char *chroot_realpath(const char *chroot, const char *path, char resolved_path[])
50+
char *chroot_realpath(const char *chroot, const char *path, char resolved_path[],size_t size_resolved_path)
5151
{
5252
char copy_path[PATH_MAX];
5353
char link_path[PATH_MAX];
@@ -135,8 +135,8 @@ char *chroot_realpath(const char *chroot, const char *path, char resolved_path[]
135135
if (n < 0) {
136136
/* If a component doesn't exist, then return what we could translate. */
137137
if (errno == ENOENT) {
138-
int ret = snprintf (resolved_path, PATH_MAX, "%s%s%s", got_path, path[0] == '/' || path[0] == '\0' ? "" : "/", path);
139-
if (ret >= PATH_MAX) {
138+
int ret = snprintf (resolved_path, size_resolved_path, "%s%s%s", got_path, path[0] == '/' || path[0] == '\0' ? "" : "/", path);
139+
if ((ret < 0 || (size_t)ret >= size_resolved_path)) {
140140
__set_errno(ENAMETOOLONG);
141141
return NULL;
142142
}

src/libcrun/criu.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
# endif
5353

5454
/* Defined in chroot_realpath.c */
55-
char *chroot_realpath (const char *chroot, const char *path, char resolved_path[]);
55+
char *chroot_realpath (const char *chroot, const char *path, char resolved_path[], size_t size_resolved_path);
5656

5757
static const char *console_socket = NULL;
5858

@@ -640,7 +640,7 @@ libcrun_container_checkpoint_linux_criu (libcrun_container_status_t *status, lib
640640
if (nofollow)
641641
return crun_make_error (err, 0, "CRIU does not support `src-nofollow` for bind mounts");
642642

643-
dest_in_root = chroot_realpath (status->rootfs, def->mounts[i]->destination, buf);
643+
dest_in_root = chroot_realpath (status->rootfs, def->mounts[i]->destination, buf, sizeof (buf));
644644
if (UNLIKELY (dest_in_root == NULL))
645645
{
646646
if (errno != ENOENT)
@@ -971,7 +971,7 @@ libcrun_container_restore_linux_criu (libcrun_container_status_t *status, libcru
971971
if (nofollow)
972972
return crun_make_error (err, 0, "CRIU does not support `src-nofollow` for bind mounts");
973973

974-
dest_in_root = chroot_realpath (status->rootfs, def->mounts[i]->destination, buf);
974+
dest_in_root = chroot_realpath (status->rootfs, def->mounts[i]->destination, buf, sizeof (buf));
975975
if (UNLIKELY (dest_in_root == NULL))
976976
{
977977
if (errno != ENOENT)

src/libcrun/ebpf.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,32 @@ struct bpf_program
6161
#ifdef HAVE_EBPF
6262

6363
# define BPF_ALU32_IMM(OP, DST, IMM) \
64-
((struct bpf_insn) { .code = BPF_ALU | BPF_OP (OP) | BPF_K, .dst_reg = DST, .src_reg = 0, .off = 0, .imm = IMM })
64+
((struct bpf_insn){ .code = BPF_ALU | BPF_OP (OP) | BPF_K, .dst_reg = DST, .src_reg = 0, .off = 0, .imm = IMM })
6565

6666
# define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
67-
((struct bpf_insn) { \
67+
((struct bpf_insn){ \
6868
.code = BPF_LDX | BPF_SIZE (SIZE) | BPF_MEM, .dst_reg = DST, .src_reg = SRC, .off = OFF, .imm = 0 })
6969

7070
# define BPF_MOV64_REG(DST, SRC) \
71-
((struct bpf_insn) { .code = BPF_ALU64 | BPF_MOV | BPF_X, .dst_reg = DST, .src_reg = SRC, .off = 0, .imm = 0 })
71+
((struct bpf_insn){ .code = BPF_ALU64 | BPF_MOV | BPF_X, .dst_reg = DST, .src_reg = SRC, .off = 0, .imm = 0 })
7272

7373
# define BPF_JMP_A(OFF) \
74-
((struct bpf_insn) { .code = BPF_JMP | BPF_JA, .dst_reg = 0, .src_reg = 0, .off = OFF, .imm = 0 })
74+
((struct bpf_insn){ .code = BPF_JMP | BPF_JA, .dst_reg = 0, .src_reg = 0, .off = OFF, .imm = 0 })
7575

7676
# define BPF_JMP_IMM(OP, DST, IMM, OFF) \
77-
((struct bpf_insn) { .code = BPF_JMP | BPF_OP (OP) | BPF_K, .dst_reg = DST, .src_reg = 0, .off = OFF, .imm = IMM })
77+
((struct bpf_insn){ .code = BPF_JMP | BPF_OP (OP) | BPF_K, .dst_reg = DST, .src_reg = 0, .off = OFF, .imm = IMM })
7878

7979
# define BPF_JMP_REG(OP, DST, SRC, OFF) \
80-
((struct bpf_insn) { .code = BPF_JMP | BPF_OP (OP) | BPF_X, .dst_reg = DST, .src_reg = SRC, .off = OFF, .imm = 0 })
80+
((struct bpf_insn){ .code = BPF_JMP | BPF_OP (OP) | BPF_X, .dst_reg = DST, .src_reg = SRC, .off = OFF, .imm = 0 })
8181

8282
# define BPF_MOV64_IMM(DST, IMM) \
83-
((struct bpf_insn) { .code = BPF_ALU64 | BPF_MOV | BPF_K, .dst_reg = DST, .src_reg = 0, .off = 0, .imm = IMM })
83+
((struct bpf_insn){ .code = BPF_ALU64 | BPF_MOV | BPF_K, .dst_reg = DST, .src_reg = 0, .off = 0, .imm = IMM })
8484

8585
# define BPF_MOV32_REG(DST, SRC) \
86-
((struct bpf_insn) { .code = BPF_ALU | BPF_MOV | BPF_X, .dst_reg = DST, .src_reg = SRC, .off = 0, .imm = 0 })
86+
((struct bpf_insn){ .code = BPF_ALU | BPF_MOV | BPF_X, .dst_reg = DST, .src_reg = SRC, .off = 0, .imm = 0 })
8787

8888
# define BPF_EXIT_INSN() \
89-
((struct bpf_insn) { .code = BPF_JMP | BPF_EXIT, .dst_reg = 0, .src_reg = 0, .off = 0, .imm = 0 })
89+
((struct bpf_insn){ .code = BPF_JMP | BPF_EXIT, .dst_reg = 0, .src_reg = 0, .off = 0, .imm = 0 })
9090
#endif
9191

9292
#ifdef HAVE_EBPF

src/libcrun/utils.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ close_and_replace (int *oldfd, int newfd)
347347
}
348348

349349
/* Defined in chroot_realpath.c */
350-
char *chroot_realpath (const char *chroot, const char *path, char resolved_path[]);
350+
char *chroot_realpath (const char *chroot, const char *path, char resolved_path[], size_t size_resolved_path);
351351

352352
static int
353353
safe_openat_fallback (int dirfd, const char *rootfs, const char *path, int flags,
@@ -359,7 +359,7 @@ safe_openat_fallback (int dirfd, const char *rootfs, const char *path, int flags
359359
size_t rootfs_len = strlen (rootfs);
360360
int ret;
361361

362-
path_in_chroot = chroot_realpath (rootfs, path, buffer);
362+
path_in_chroot = chroot_realpath (rootfs, path, buffer, sizeof (buffer));
363363
if (path_in_chroot == NULL)
364364
return crun_make_error (err, errno, "cannot resolve `%s` under rootfs", path);
365365

@@ -2297,9 +2297,9 @@ copy_recursive_fd_to_fd (int srcdirfd, int dfd, const char *srcname, const char
22972297
if (UNLIKELY (ret < 0))
22982298
return crun_make_error (err, errno, "fchownat `%s/%s`", destname, de->d_name);
22992299

2300-
/*
2301-
* ALLPERMS is not defined by POSIX
2302-
*/
2300+
/*
2301+
* ALLPERMS is not defined by POSIX
2302+
*/
23032303
#ifndef ALLPERMS
23042304
# define ALLPERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
23052305
#endif

tests/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ memhog (int megabytes)
370370
while (1)
371371
{
372372
/* change one page each 0.1 seconds */
373-
nanosleep ((const struct timespec[]) { { 0, 100000000L } }, NULL);
373+
nanosleep ((const struct timespec[]){ { 0, 100000000L } }, NULL);
374374
buf[pos] = 'c';
375375
pos += sysconf (_SC_PAGESIZE);
376376
if (pos > megabytes * 1024 * 1024)

tests/tests_libcrun_fuzzer.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ test_generate_ebpf (uint8_t *buf, size_t len)
111111
return 0;
112112
}
113113

114-
char *chroot_realpath (const char *chroot, const char *path, char resolved_path[]);
114+
char *chroot_realpath (const char *chroot, const char *path, char resolved_path[], size_t size_resolved_path);
115115

116116
static int
117117
test_chroot_realpath (uint8_t *buf, size_t len)
@@ -123,7 +123,7 @@ test_chroot_realpath (uint8_t *buf, size_t len)
123123
if (path == NULL)
124124
return 0;
125125

126-
chroot_realpath (".", path, resolved_path);
126+
chroot_realpath (".", path, resolved_path, sizeof (resolved_path));
127127
(void) resolved_path;
128128
return 0;
129129
}
@@ -496,7 +496,7 @@ main (int argc, char **argv)
496496
return LLVMFuzzerTestOneInput (content, len);
497497
}
498498
#ifdef FUZZER
499-
extern void HF_ITER (uint8_t **buf, size_t *len);
499+
extern void HF_ITER (uint8_t * *buf, size_t * len);
500500
for (;;)
501501
{
502502
size_t len;

0 commit comments

Comments
 (0)