Commit Graph

1662 Commits

Author SHA1 Message Date
David Xu
17001e0b94 Make more code be protected by internal mutex, and now it is fork-safe, in
error case, the file exclusive lock is now released as soon as possible,
in previous code, child process can still hold the exclusive lock.
2013-02-17 02:52:42 +00:00
Pawel Jakub Dawidek
1f2ce2a086 Put one file per line so it is easier to read diffs against those files. 2013-02-16 22:21:46 +00:00
David Xu
a9c09aee21 Simplify code by using flag O_EXLOCK.
PR: kern/175674
2013-02-16 06:07:07 +00:00
Jilles Tjoelker
ad7d20c921 fts: Use O_DIRECTORY when opening name that might be changed by attacker.
There are uncommon cases where fts_safe_changedir() may be called with a
non-NULL name that is not "..". Do not block or worse if an attacker put (a
(symlink to) a fifo or device where a directory used to be.

MFC after:	1 week
2013-02-10 23:09:15 +00:00
Brooks Davis
62557825cc Add contrib/libc-vis to the include path so we reliably pick up the right
version of vis.h.

Reported by:	dim
2013-01-11 17:51:03 +00:00
Brooks Davis
30bd63bc4a In r244401 I accidently moved strunvis and strunvisx from version 1.0 to
1.3 breaking the libc ABI. Revert that change (breaking the ABI again
for users who updated after December 18th).
2013-01-11 15:50:01 +00:00
Xin LI
53e075c0b9 - Reduce buffer size from LINE_MAX to PATH_MAX, there is no point to store
path longer than this.
 - Fix an unreached case of check against sizeof buf, which in turn leads
   to an off-by-one nul byte write on the stack.  The original condition
   can never be satisfied because the passed boundary is the maximum value
   that can be returned, so code was harmless.

MFC after:	1 month
2012-12-21 22:20:59 +00:00
Brooks Davis
8ccca1222d Replace our implementation of the vis(3) and unvis(3) APIs with
NetBSD's.  This output size limited versions of vis and unvis functions
as well as a set of vis variants that allow arbitrary characters to be
specified for encoding.

Finally, MIME Quoted-Printable encoding as described in RFC 2045 is
supported.
2012-12-18 16:37:24 +00:00
Pawel Jakub Dawidek
997b5d9426 Eliminate redundant variable. 2012-12-12 15:27:33 +00:00
Jilles Tjoelker
1084b38bea libc: Make various internal file descriptors close-on-exec.
These are obtained via fopen().
2012-12-10 17:56:51 +00:00
Jilles Tjoelker
273e3bbfe6 libc: Use the new 'e' fopen() mode option to simplify fstab.c.
No functional change is intended.
2012-12-04 16:54:43 +00:00
Marcel Moolenaar
daab0b01ed Protect against DoS attacks, such as being described in CVE-2010-2632.
The changes were derived from what has been committed to NetBSD, with
modifications. These are:
1.  Preserve the existsing GLOB_LIMIT behaviour by including the number
    of matches to the set of parameters to limit.
2.  Change some of the limits to avoid impacting normal use cases:
    GLOB_LIMIT_STRING - change from 65536 to ARG_MAX so that glob(3)
	can still provide a full command line of expanded names.
    GLOB_LIMIT_STAT - change from 128 to 1024 for no other reason than
	that 128 feels too low (it's not a limit that impacts the
	behaviour of the test program listed in CVE-2010-2632).
    GLOB_LIMIT_PATH - change from 1024 to 65536 so that glob(3) can
	still provide a fill command line of expanded names.
3.  Protect against buffer overruns when we hit the GLOB_LIMIT_STAT or
    GLOB_LIMIT_READDIR limits. We append SEP and EOS to pathend in
    those cases. Return GLOB_ABORTED instead of GLOB_NOSPACE when we
    would otherwise overrun the buffer.

This change also modifies the existing behaviour of glob(3) in case
GLOB_LIMIT is specifies by limiting the *new* matches and not all
matches. This is an important distinction when GLOB_APPEND is set or
when the caller uses a non-zero gl_offs. Previously pre-existing
matches or the value of gl_offs would be counted in the number of
matches even though the man page states that glob(3) would return
GLOB_NOSPACE when gl_matchc or more matches were found.

The limits that cannot be circumvented are GLOB_LIMIT_STRING and
GLOB_LIMIT_PATH all others can be crossed by simply calling glob(3)
again and with GLOB_APPEND set.

The entire description above applies only when GLOB_LIMIT has been
specified of course. No limits apply when this flag isn't set!

Obtained from: Juniper Networks, Inc
2012-12-01 21:26:46 +00:00
Marcel Moolenaar
43cc14e088 In globextend(), take advantage of the fact that realloc(NULL, size) is
equivalent to malloc(size). This eliminates the conditional expression
used for calling either realloc() or malloc() when realloc() will do
all the time.
2012-12-01 17:50:39 +00:00
Marcel Moolenaar
b628fac5ea In globextend() when the pathv vector cannot be (re-)allocated, don't
free and clear the gl_pathv pointer in the glob_t structure. Such
breaks the invariant of the glob_t structure, as stated in the comment
right in front of the globextend() function. If gl_pathv was non-NULL,
then gl_pathc was > 0. Making gl_pathv a NULL pointer without also
setting gl_pathc to 0 is wrong.

Since we otherwise don't free the memory associated with a glob_t in
error cases, it's unlikely that this change will cause a memory leak
that wasn't already there to begin with. Callers of glob(3) must
call globfree(3) irrespective of whether glob(3) returned an error
or not.
2012-12-01 17:44:06 +00:00
Joel Dahl
90c819e0a0 mdoc: Use the Ev macro for environmental variables. 2012-11-16 12:03:50 +00:00
Greg Lehey
c90e931745 Complete man page.
MFC after:	2 weeks
2012-11-16 01:41:42 +00:00
Konstantin Belousov
eb3d4e1fbd Implement the waitid() SUSv4 function using wait6() system call.
PR:	standards/170346
Submitted by:	"Jukka A. Ukkonen" <jau@iki.fi>
MFC after:	1 month
2012-11-13 12:55:52 +00:00
Dimitry Andric
34a4971299 Add an explanatory comment to lib/libc/gen/isnan.c about the fix to make
static linking with libc and libm work.

Requested by:	jilles
MFC after:	1 week
X-MFC-With:	242879
2012-11-11 13:28:04 +00:00
Dimitry Andric
0779690c2e Only define isnan, isnanf, __isnan and __isnanf in libc.so, not in
libc.a and libc_p.a.  In addition, define isnan in libm.a and libm_p.a,
but not in libm.so.

This makes it possible to statically link executables using both isnan
and isnanf with libc and libm.

Tested by:	kargl
MFC after:	1 week
2012-11-10 21:22:10 +00:00
Brooks Davis
4c0d7cdf5d Replace our version of the pwcache(3) API with NetBSD's implementation.
This adds two features:
 * uid_from_user() and gid_from_group() as the reverse of user_from_uid()
   and groups_from_gid().
 * pwcache_userdb() and pwcache_groupdb() which allow alternative lookup
   functions to be used.  For example lookups from passwd and group
   databases in a non-standard location.
2012-10-19 12:44:22 +00:00
Stefan Farfeleder
1dcbb99523 Fix my last commit. Only call strunvis after properly checking the argument is
not NULL.
2012-10-11 08:18:28 +00:00
Stefan Farfeleder
da0878836b Decode the first two fstab fields with strunvis(3). This allows having spaces
in devices and mount paths, encoded as \s or \040.

PR:		bin/117687
Submitted by:	Martin Kammerhofer
Discussed on:	arch
2012-10-11 07:54:29 +00:00
Stefan Farfeleder
a31c8b58b5 Apply some style. Checked with md5. 2012-10-11 07:39:51 +00:00
Jilles Tjoelker
05eb11cbc4 libc: Use O_CLOEXEC for various internal file descriptors.
This fixes a race condition where another thread may fork() before CLOEXEC
is set, unintentionally passing the descriptor to the child process.

This commit only adds O_CLOEXEC flags to open() or openat() calls where no
fcntl(fd, F_SETFD, FD_CLOEXEC) follows. The separate fcntl() call still
leaves a race window so it should be fixed later.
2012-09-29 11:54:34 +00:00
Jilles Tjoelker
6b42272163 libc/fts: Use O_CLOEXEC for internal file descriptors.
Because fts keeps internal file descriptors open across calls, making such
descriptors close-on-exec helps not only multi-threaded applications but
also single-threaded applications.

In particular, this prevents passing a temporary file descriptor for saving
the current directory to processes created via find -exec.
2012-09-27 22:05:54 +00:00
Dag-Erling Smørgrav
f7668359de Add the same warning to rand48(3) as to rand(3) and random(3).
MFC after:	3 days
2012-09-11 12:55:15 +00:00
Tom Rhodes
4d8ed60cb4 Avoid segfault if name is invalid. Basically, only
check for CTL_USER if the sysctl fails with ENOENT.

PR:		169056
Reviewed by:	jhb
2012-09-06 20:15:44 +00:00
Joel Dahl
abf0999093 Remove trailing whitespace. 2012-08-21 13:33:48 +00:00
David Xu
9653e55f6a Add manual pages for clock_getcpuclockid and pthread_getcpuclockid. 2012-08-21 09:18:28 +00:00
David Xu
cdfe902650 Fix prototype. Also the function should return error code instead of
-1 on error.
2012-08-21 09:17:13 +00:00
David Xu
d65f1abca7 Implement syscall clock_getcpuclockid2, so we can get a clock id
for process, thread or others we want to support.
Use the syscall to implement POSIX API clock_getcpuclock and
pthread_getcpuclockid.

PR:	168417
2012-08-17 02:26:31 +00:00
Ed Schouten
19c79bf86a Rename aux.c to auxv.c.
On Windows, AUX is the auxiliary device, usually pointing to COM1.
Therefore it is forbidden to create a file named aux.c. To make it a bit
easier for Windows users to check out our source code, rename this file
to auxv.c.

MFC after:	1 month
Discussed with:	kib
Suggested by:	Eric van Gyzen <eric vangyzen net>
2012-08-11 12:07:24 +00:00
Jilles Tjoelker
9d8ca3b2ad nftw(): POSIX says directories causing loops should be silently skipped.
Formerly, loops caused nftw() to abort the traversal with ELOOP.
2012-08-09 22:05:40 +00:00
Xin LI
81b0715a13 Refresh with OpenBSD RCS ID changes to reflect that we now have essentionally
the same file.
2012-08-09 19:22:54 +00:00
Jilles Tjoelker
47875b0caa ftw(): Do not check the maxfds argument against OPEN_MAX.
Apart from the fact that nothing should have OPEN_MAX as a limit (as opposed
to RLIMIT_NOFILE from getrlimit() or _SC_OPEN_MAX from sysconf()), POSIX
does not require us to check this. POSIX does have a requirement on the
application that maxfds not exceed {OPEN_MAX}, but does not require the
implementation to check it ("may fail").

PR:		95239
2012-08-09 15:11:38 +00:00
Jilles Tjoelker
4d767dfea4 nftw(): Do not check the maxfds argument against OPEN_MAX.
Apart from the fact that nothing should have OPEN_MAX as a limit (as opposed
to RLIMIT_NOFILE from getrlimit() or _SC_OPEN_MAX from sysconf()), POSIX
does not require us to check this.

PR:		95239
Submitted by:	Todd Miller
2012-08-09 15:04:06 +00:00
Xin LI
0dfbbb3391 Use calloc(). 2012-08-01 00:21:55 +00:00
Brooks Davis
9597393516 Document the behavior (from 4.4-lite) that tokens returned by telldir() are
single use.

Sponsored by:	DARPA, AFRL
MFC after:	3 days
2012-07-05 17:02:20 +00:00
Pawel Jakub Dawidek
72de35d0a7 Prefer sysctl to open/read/close for obtaining random data.
This method is more sandbox-friendly and also should be faster as only
one syscall is needed instead of three.
In case of an error fall back to the old method.

Reviewed by:	simon, gleb
MFC after:	2 weeks
2012-07-04 19:51:25 +00:00
Konstantin Belousov
a87855124c Optimize the handling of SC_NPROCESSORS_CONF, by using auxv AT_NCPU
value if present.

MFC after:	1 week
2012-06-27 20:32:45 +00:00
Eitan Adler
43be4ab038 Remove the assert added in r237286
The use of assertions in libraries is not widely accepted.

Requested by:	bde, scottl
Approved by:	cperciva
MFC after:	3 days
X-MFC-With:	r237286
2012-06-24 07:03:22 +00:00
Konstantin Belousov
869fd80fd4 Use struct vdso_timehands data to implement fast gettimeofday(2) and
clock_gettime(2) functions if supported. The speedup seen in
microbenchmarks is in range 4x-7x depending on the hardware.

Only amd64 and i386 architectures are supported. Libc uses rdtsc and
kernel data to calculate current time, if enabled by kernel.

Hopefully, this code is going to migrate into vdso in some future.

Discussed with:	bde
Reviewed by:	jhb
Tested by:	flo
MFC after:	1 month
2012-06-22 07:13:30 +00:00
Eitan Adler
bf36cf8e1b Don't close an uninitialized descriptor. [1]
Add a sanity check for the validity of the passed fd.

PR:		kern/139080 [1]
Submitted by:	Andrey Simonenko <simon@comsys.ntu-kpi.kiev.ua> [1]
Reviewed by:	pjd (briefly)
Approved by:	cperciva
MFC after:	1 week
2012-06-20 06:38:41 +00:00
Konstantin Belousov
17ec7b7aa8 More style.
MFC after:	3 days
2012-06-16 13:11:10 +00:00
Konstantin Belousov
3a3c91219b Revert part of the r235740 which changed separate allocation of the
string buffer for each linelist l_line into one large string. Since
linelists parsed out during the previous passes store the pointers to
previously allocated l_lines, the reallocation caused undefined
behaviour on accessing the buffers, and quite deterministic fault on
freeing them (in mountd(8) startup).

This fixes reading of netgroup(5) file which contains more then one
netgroup.

Discussed with:	ghelmer
MFC after:	3 days
2012-06-16 13:10:22 +00:00
Joel Dahl
15e92497e2 mandoc fixes.
Obtained from:	OpenBSD
2012-06-16 06:38:11 +00:00
Konstantin Belousov
61235d9e01 Make sure that fstab fd is not leaked on exec.
PR:  kern/169023
Submitted by:	Jukka Ukkonen <jau iki fi>
MFC after:	1 week
2012-06-14 12:28:43 +00:00
Joel Dahl
9ee2158b55 mdoc: minor Bl improvements. 2012-06-02 08:47:26 +00:00
Guy Helmer
3d1d73c22f Style(9) improvements: remove unnecessary parenthesis, improve order
of local variable declarations, remove bogus casts, and resolve long
lines.

Reviewed by:	bde
2012-06-01 15:02:23 +00:00
Guy Helmer
fed7420ced Add checks for memory allocation failures in appropriate places, and
avoid creating bad entries in the grp list as a result of memory allocation
failures while building new entries.

PR:		bin/83340
Reviewed by:	delphij (prior version of patch)
2012-05-21 21:10:00 +00:00