Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__readlink_chk, __readlinkat_chk: properly pass buffer length #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yorickvP
Copy link

Fixes #114

Copy link

@josch josch left a comment

Choose a reason for hiding this comment

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

This effectively circumvents the check that is done in __readlink_chk which checks if (len > buflen) by passing FAKECHROOT_PATH_MAX-1 for both arguments.

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

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

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

@@ -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)) {
printf("readlink: prevented write past end of buffer\n");
Copy link

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

@@ -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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

buffer-overflow FORTIFY_SOURCE=2
3 participants