freebsd-dev/bin
Kirk McKusick 927f8d8bbb Handle NULL return from localtime(3) in ls(1) and find(1)
The ls(1) (with -l option) and find(1) (with -ls option) utilties
segment fault when operating on files with very large modification
times. A recent disk corruption set a spurious bit in the mtime
field of one of my files to 0x8000000630b0167 (576460753965089127)
which is in year 18,266,940,962. I discovered the problem when
running fsck_ffs(8) which uses ctime(3) to convert it to a readable
format. Ctime cannot fit the year into its four character field, so
returns ??? ??? ?? ??:??:?? ???? (typically Thu Nov 24 18:22:48 2021).

With the filesystem mounted, I used `ls -l' to see how it would
report the modification time and it segment faulted. The find(1)
program also segment faulted (see script below). Both these utilities
call the localtime(3) function to decode the modification time.
Localtime(3) returns a pointer to a struct tm (which breaks things
out into its component pieces: year, month, day, hour, minute,
second). The ls(1) and find(1) utilities then print out the date
based on the appropriate fields in the returned tm structure.

Although not documented in the localtime(3) manual page, localtime(3)
returns a NULL pointer if the passed in time translates to a year
that will not fit in an "int" (which if "int" is 32-bits cannot
hold the year 18,266,940,962). Since ls(1) and find(1) do not check
for a NULL struct tm * return from localtime(3), they segment fault
when they try to dereference it.

When localtime(3) returns NULL, the attached patches produce a date
string of "bad date val". This string is chosen because it has the
same number of characters (12) and white spaces (2) as the usual
date string, for example "Sep 3 22:06" or "May 15 2017".

The most recent ANSI standard for localtime(3) does say that localtime(3)
can return NULL (see https://pubs.opengroup.org/onlinepubs/9699919799/
and enter localtime in the search box). Our localtime(3) man page should
be updated to indicate that NULL is a possible return. More importantly,
there are over 100 uses of localtime(3) in the FreeBSD source tree (see
Differential Revision D36474 for the list). Most do not check for a NULL
return from localtime(3).

Reported by:  Peter Holm
Reviewed by:  kib, Chuck Silvers, Warner Losh
MFC after:    2 weeks
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D36474
2022-09-09 14:30:42 -07:00
..
cat cat: style nits 2021-01-20 18:46:26 +01:00
chflags fts_read: Handle error from a NULL return better. 2020-12-08 23:38:26 +00:00
chio pkgbase: Put chio in utilities 2021-06-19 17:49:44 +02:00
chmod fts_read: Handle error from a NULL return better. 2020-12-08 23:38:26 +00:00
cp cp: Fix build without VM_AND_BUFFER_CACHE_SYNCHRONIZED. 2022-08-30 10:51:21 -04:00
csh pkgbase: Create a FreeBSD-csh package 2021-12-21 10:17:50 +01:00
date date: remove some ambiguity in tzdata references 2022-07-28 10:14:18 -05:00
dd Add O_DIRECT flag to DD for cache bypass 2020-06-04 20:47:11 +00:00
df df: do not report a 100% full inode usage on fs without inodes 2022-03-11 08:49:44 +01:00
domainname pkgbase: Put yellow pages programs to its own package 2021-12-21 10:17:22 +01:00
echo echo(1): Replace errexit() with err(3) 2021-11-09 22:12:19 +01:00
ed ed(1): Add two references in the SEE ALSO section 2021-04-09 09:43:49 +02:00
expr expr(1): Fix overflow detection when operand is INTMAX_MIN 2018-04-14 04:35:10 +00:00
freebsd-version freebsd-version(1): Add -j flag to support jails 2021-10-01 13:50:56 -05:00
getfacl Fix build of bin/getfacl after libc changes. 2021-08-27 11:52:18 +03:00
hostname hostname: avoid strcpy() overlap in -d flag handling 2021-09-25 00:03:50 -05:00
kenv kenv: allow listing of static kernel environments 2021-07-18 23:06:19 -05:00
kill General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
ln General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
ls Handle NULL return from localtime(3) in ls(1) and find(1) 2022-09-09 14:30:42 -07:00
mkdir src/bin/mkdir: Spell occur correctly. 2021-10-02 10:39:31 -06:00
mv General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
pax For man page references found in ports, indicate the respective port. 2022-08-14 15:02:06 +02:00
pkill bin/pkill: Fix {pgrep,pkill}-j_test.sh 2021-02-13 13:53:51 +00:00
ps ps(1): Use calloc instead of malloc and memset. 2022-03-11 10:36:00 +01:00
pwait Fix typo in pwait.c introduced in 5bdce6ff54 2021-01-21 21:43:27 +00:00
pwd pwd(1): Add EXAMPLES section 2020-10-24 16:42:35 +00:00
realpath realpath(1): Add EXAMPLES section. 2020-10-20 13:15:26 +00:00
rm fts_read: Handle error from a NULL return better. 2020-12-08 23:38:26 +00:00
rmail DIRDEPS_BUILD: Update dependencies. 2017-10-31 00:07:04 +00:00
rmdir General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
setfacl Remove unused headers. 2020-12-12 02:26:43 +00:00
sh sh: nullify ENV in tests 2022-08-20 13:27:42 +02:00
sleep sleep.1: Fix mdoc style 2022-05-25 10:11:33 +02:00
stty stty: Remove stray '-'. 2022-05-24 14:31:28 -06:00
sync DIRDEPS_BUILD: Update dependencies. 2017-10-31 00:07:04 +00:00
test Remove unused includes. 2020-12-22 21:04:36 -08:00
tests Merge ^/user/ngie/release-pkg-fix-tests to unbreak how test files are installed 2016-05-04 23:20:53 +00:00
uuidgen pkgbase: Create a FreeBSD-utilities package and make it the default one 2019-09-05 14:15:47 +00:00
Makefile Remove rcmds. 2017-10-06 08:43:14 +00:00
Makefile.inc build: provide a default WARNS for all in-tree builds 2020-09-18 17:17:46 +00:00