Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/__readlink_chk.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define _FORTIFY_SOURCE 2
#include <stddef.h>
#include <unistd.h>
#include <stdio.h>
#include "libfakechroot.h"


Expand All @@ -41,7 +42,12 @@ wrapper(__readlink_chk, ssize_t, (const char * path, char * buf, size_t bufsiz,
debug("__readlink_chk(\"%s\", &buf, %zd, %zd)", path, bufsiz, buflen);
expand_chroot_path(path);

if ((linksize = nextcall(__readlink_chk)(path, tmp, FAKECHROOT_PATH_MAX-1, buflen)) == -1) {
if (__builtin_expect(!!(bufsiz > buflen), 0)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the double exclamation mark? bufsiz > buflen is already a boolean

printf("readlink: prevented write past end of buffer\n");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the printf to stdout? at most there is a print to stderr done in the rest of the codebase

exit(-1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the exit? why not just return failure?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because _chk functions should exit on overflow. Actually __chk_fail should be called (similar to __realpath_chk).
https://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/libc---readlink-chk-1.html

}

if ((linksize = nextcall(__readlink_chk)(path, tmp, FAKECHROOT_PATH_MAX-1, FAKECHROOT_PATH_MAX-1)) == -1) {
return -1;
}
tmp[linksize] = '\0';
Expand Down
8 changes: 7 additions & 1 deletion src/__readlinkat_chk.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#define _FORTIFY_SOURCE 2
#include <stddef.h>
#include <unistd.h>
#include <stdio.h>
#include "libfakechroot.h"


Expand All @@ -42,7 +43,12 @@ wrapper(__readlinkat_chk, ssize_t, (int dirfd, const char * path, char * buf, si
debug("__readlinkat_chk(%d, \"%s\", &buf, %zd, %zd)", dirfd, path, bufsiz, buflen);
expand_chroot_path_at(dirfd, path);

if ((linksize = nextcall(__readlinkat_chk)(dirfd, path, tmp, FAKECHROOT_PATH_MAX-1, buflen)) == -1) {
if (__builtin_expect(!!(bufsiz > buflen), 0)) {
printf("readlinkat: prevented write past end of buffer\n");
exit(-1);
}

if ((linksize = nextcall(__readlinkat_chk)(dirfd, path, tmp, FAKECHROOT_PATH_MAX-1, FAKECHROOT_PATH_MAX-1)) == -1) {
return -1;
}
tmp[linksize] = '\0';
Expand Down