glibc/misc
Joan Bruguera ca0faa140f misc: Fix rare fortify crash on wchar funcs. [BZ 29030]
If `__glibc_objsize (__o) == (size_t) -1` (i.e. `__o` is unknown size), fortify
checks should pass, and `__whatever_alias` should be called.

Previously, `__glibc_objsize (__o) == (size_t) -1` was explicitly checked, but
on commit a643f60c53, this was moved into `__glibc_safe_or_unknown_len`.

A comment says the -1 case should work as: "The -1 check is redundant because
since it implies that __glibc_safe_len_cond is true.". But this fails when:
* `__s > 1`
* `__osz == -1` (i.e. unknown size at compile time)
* `__l` is big enough
* `__l * __s <= __osz` can be folded to a constant
(I only found this to be true for `mbsrtowcs` and other functions in wchar2.h)

In this case `__l * __s <= __osz` is false, and `__whatever_chk_warn` will be
called by `__glibc_fortify` or `__glibc_fortify_n` and crash the program.

This commit adds the explicit `__osz == -1` check again.
moc crashes on startup due to this, see: https://bugs.archlinux.org/task/74041

Minimal test case (test.c):
    #include <wchar.h>

    int main (void)
    {
        const char *hw = "HelloWorld";
        mbsrtowcs (NULL, &hw, (size_t)-1, NULL);
        return 0;
    }

Build with:
    gcc -O2 -Wp,-D_FORTIFY_SOURCE=2 test.c -o test && ./test

Output:
    *** buffer overflow detected ***: terminated

Fixes: BZ #29030
Signed-off-by: Joan Bruguera <joanbrugueram@gmail.com>
Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
(cherry picked from commit 33e03f9cd2)
2022-04-25 18:44:27 +05:30
..
bits
sys misc: Fix rare fortify crash on wchar funcs. [BZ 29030] 2022-04-25 18:44:27 +05:30
acct.c
allocate_once.c
ar.h
brk.c
bug18240.c
bug-hsearch1.c
chflags.c
chroot.c
daemon.c
dirname.c
efgcvt_r-template.c
efgcvt_r.c
efgcvt-dbl-macros.h
efgcvt-ldbl-macros.h
efgcvt-template.c
efgcvt.c
err.c
err.h
error.c nptl: Move pthread_setcancelstate into libc 2021-04-21 19:49:50 +02:00
error.h
fchflags.c
fd_to_filename.c misc: use _fitoa_word to implement __fd_to_filename. 2021-05-07 13:54:36 -03:00
fdatasync.c Add hidden prototypes for fsync, fdatasync 2021-06-22 09:51:14 +02:00
fgetxattr.c
flistxattr.c
fremovexattr.c
fsetxattr.c
fstab.c
fstab.h
fsync.c Add hidden prototypes for fsync, fdatasync 2021-06-22 09:51:14 +02:00
ftruncate64.c
ftruncate.c
futimes.c
futimesat.c
getauxval.c
getclktck.c
getdomain.c
getdtsz.c
gethostid.c
gethostname.c
getloadavg.c
getpagesize.c
getpass.c
getsysstats.c misc: Add __get_nprocs_sched 2021-10-01 18:19:09 +02:00
getttyent.c
getusershell.c
getxattr.c
gtty.c
hsearch_r.c
hsearch.c
ifunc-impl-list.c
init-misc.c
insremque.c
ioctl.c
lgetxattr.c
libgen.h
listxattr.c
llistxattr.c
lremovexattr.c
lsearch.c
lsetxattr.c
lutimes.c
madvise.c
makedev.c
Makefile Move malloc hooks into a compat DSO 2021-07-22 18:37:59 +05:30
mincore.c
mkdtemp.c
mkostemp64.c
mkostemp.c
mkostemps64.c
mkostemps.c
mkstemp64.c
mkstemp.c
mkstemps64.c
mkstemps.c
mktemp.c nptl: Move sem_close, sem_open into libc 2021-05-05 17:19:38 +02:00
mlock.c
mlockall.c
mmap64.c
mmap.c
mntent_r.c
mntent.c
mntent.h
mprotect.c
msync.c
munlock.c
munlockall.c
munmap.c
preadv2.c
preadv64.c
preadv64v2.c
preadv.c
pselect.c
ptrace.c
pwritev2.c
pwritev64.c
pwritev64v2.c
pwritev.c
qefgcvt_r.c
qefgcvt.c
readv.c
reboot.c
regexp.c Force building with -fno-common 2021-07-09 20:09:14 +02:00
regexp.h
remap_file_pages.c
removexattr.c
revoke.c
sbrk.c
search.h
select.c
setdomain.c
setegid.c
seteuid.c
sethostid.c
sethostname.c
setregid.c
setreuid.c
setxattr.c
sgtty.h
single_threaded.c
sstk.c
stab.h
stty.c
swapoff.c
swapon.c
sync.c
syncfs.c
syscall.c
syscall.h
sysexits.h
syslog.c misc: syslog: Use static const for AF_UNIX address 2021-04-15 11:32:40 -03:00
syslog.h
truncate64.c
truncate.c
tsearch.c
tst-allocate_once.c
tst-atomic-long.c
tst-atomic.c
tst-dbl-efgcvt.c
tst-dirname.c
tst-efgcvt-template.c
tst-empty.c
tst-error1.c
tst-fd_to_filename.c
tst-fdset.c
tst-gethostid.c
tst-hsearch.c
tst-insremque.c
tst-ioctl-time64.c misc: Add time64 alias for ioctl 2021-07-21 11:58:09 +02:00
tst-ioctl.c misc: Add time64 alias for ioctl 2021-07-21 11:58:09 +02:00
tst-ldbl-efgcvt.c
tst-ldbl-error.c
tst-ldbl-warn.c
tst-makedev.c
tst-mntent2.c
tst-mntent-autofs.c
tst-mntent-blank-corrupt.c
tst-mntent-blank-passno.c
tst-mntent-escape.c
tst-mntent.c
tst-preadvwritev2-common.c
tst-preadvwritev2.c
tst-preadvwritev64.c
tst-preadvwritev64v2.c
tst-preadvwritev-common.c
tst-preadvwritev.c
tst-pselect-time64.c y2038: Add test coverage 2021-06-15 10:42:11 -03:00
tst-pselect.c linux: Only use 64-bit syscall if required for pselect 2021-06-22 12:09:52 -03:00
tst-select-time64.c y2038: Add test coverage 2021-06-15 10:42:11 -03:00
tst-select.c linux: Only use 64-bit syscall if required for select 2021-06-22 12:09:52 -03:00
tst-syscalls.c
tst-tsearch.c
tst-warn-wide.c
ttyent.h
ttyslot.c
ualarm.c
unwind-link.c
usleep.c
ustat.c
utimes.c
Versions
vhangup.c
writev.c