Commit Graph

19 Commits

Author SHA1 Message Date
Pedro F. Giffuni
d915a14ef0 libc: further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 2-Clause license, however the tool I
was using mis-identified many licenses so this was mostly a manual - error
prone - task.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.
2017-11-25 17:12:48 +00:00
Pedro F. Giffuni
9f36610f9e libc: provide some bounds-checking through reallocarray(3).
reallocarray(3) is a non portable extension that originated in OpenBSD.
Given that it is already in FreeBSD's libc it is useful for the cases
where reallocation involves a multiplication.

MFC after:		2 weeks
Differential Revision:	https://reviews.freebsd.org/D9955
2017-03-12 16:03:34 +00:00
Jilles Tjoelker
d358fa780b wordexp: Rewrite to make WRDE_NOCMD reliable.
Shell syntax is too complicated to detect command substitution and unquoted
operators reliably without implementing much of sh's parser. Therefore, have
sh do this detection.

While changing sh's support anyway, also read input from a pipe instead of
arguments to avoid {ARG_MAX} limits and improve privacy, and output count
and length using 16 instead of 8 digits.

The basic concept is:
execl("/bin/sh", "sh", "-c", "freebsd_wordexp ${1:+\"$1\"} -f "$2",
    "", flags & WRDE_NOCMD ? "-p" : "", <pipe with words>);

The WRDE_BADCHAR error is still implemented in libc. POSIX requires us to
fail strings containing unquoted braces with code WRDE_BADCHAR. Since this
is normally not a syntax error in sh, there is still a need for checking
code in libc, we_check().

The new we_check() is an optimistic check that all the characters
  <newline> | & ; < > ( ) { }
are quoted. To avoid duplicating too much sh logic, such characters are
permitted when quoting characters are seen, even if the quoting characters
may themselves be quoted. This code reports all WRDE_BADCHAR errors; bad
characters that get past it and are a syntax error in sh return WRDE_SYNTAX.

Although many implementations of WRDE_NOCMD erroneously allow some command
substitutions (and ours even documented this), there appears to be code that
relies on its security (codesearch.debian.net shows quite a few uses).
Passing untrusted data to wordexp() still exposes a denial of service
possibility and a fairly large attack surface.

Reviewed by:	wblock (man page only)
MFC after:	2 weeks
Relnotes:	yes
Security:	fixes command execution with wordexp(untrusted, WRDE_NOCMD)
2015-09-30 21:32:29 +00:00
Konstantin Belousov
bd6060a1c6 Switch libc from using _sig{procmask,action,suspend} symbols, which
are aliases for the syscall stubs and are plt-interposed, to the
libc-private aliases of internally interposed sigprocmask() etc.

Since e.g. _sigaction is not interposed by libthr, calling signal()
removes thr_sighandler() from the handler slot etc.  The result was
breaking signal semantic and rtld locking.

The added __libc_sigprocmask and other symbols are hidden, they are
not exported and cannot be called through PLT.  The setjmp/longjmp
functions for x86 were changed to use direct calls, and since
PIC_PROLOGUE only needed for functional PLT indirection on i386, it is
removed as well.

The PowerPC bug of calling the syscall directly in the setjmp/longjmp
implementation is kept as is.

Reported by:	Pete French <petefrench@ingresso.co.uk>
Tested by:	Michiel Boland <boland37@xs4all.nl>
Reviewed by:	jilles (previous version)
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2015-08-29 14:25:01 +00:00
Jilles Tjoelker
89cead337a wordexp(): Improve some error codes.
Distinguish between WRDE_BADVAL and WRDE_SYNTAX based on when the error
occurred (parsing or execution), not based on whether WRDE_UNDEF was passed.

Also, return WRDE_NOSPACE for a few more unexpected results from sh.
2015-08-19 20:31:03 +00:00
Jilles Tjoelker
842ad8ac0b wordexp(): Stop using the undocumented wordexp builtin.
The functionality of the wordexp builtin is easily replaced using normal
shell code, although performance is slightly worse.

This does not mean that wordexp() will remain shell-independent -- a fully
reliable implementation of WRDE_NOCMD is really only possible using
extensions to the shell, or by adding much of the shell's code to libc.
2015-08-16 19:42:15 +00:00
Jilles Tjoelker
2f61288c80 wordexp: Explicitly pass along IFS.
Per Austin group issue #884, sh should not import IFS from the environment
but always set it to $' \t\n'. For wordexp(), however, it is documented and
useful for it to use IFS from the environment.

Since sh currently imports IFS from the environment, this change has no
functional effect.

MFC after:	1 week
2015-03-31 20:51:01 +00:00
Jilles Tjoelker
f6d7148dc3 wordexp(): Avoid leaking the pipe file descriptors to a parallel fork/exec.
This uses the new pipe2() system call added on May 1 (r250159).
2013-08-27 21:47:01 +00:00
Jilles Tjoelker
8d0f6b5fc2 wordexp(): Fix syntax validation for backslashes in single-quotes. 2013-07-23 21:09:26 +00:00
Jilles Tjoelker
ae4c676c55 wordexp(): Simplify code by deferring work to sh. 2013-05-09 11:43:45 +00:00
Jilles Tjoelker
70d41b5846 wordexp(): Remove wrong IFS usage.
Words in shell script are separated by spaces or tabs independent of the
value of IFS. The value of IFS is only relevant for the result of
substitutions. Therefore, there should be a space between 'wordexp' and the
words to be expanded, not an IFS character.

Paranoia might dictate that the shell ignore IFS from the environment (even
though our sh currently uses it), so do not depend on it in the new test
case.
2013-04-01 20:50:07 +00:00
Garrett Wollman
0c0349bfa4 Eliminate more dead stores.
Found by:	Clang static analyzer
MFC after:	7 days
2009-11-25 04:45:45 +00:00
Jilles Tjoelker
364e9ccb9c wordexp(3): fix some bugs with signals and long outputs
* retry various system calls on EINTR
* retry the rest after a short read (common if there is more than about 1K
  of output)
* block SIGCHLD like system(3) does (note that this does not and cannot
  work fully in threaded programs, they will need to be careful with wait
  functions)

PR:		90580
MFC after:	1 month
2009-10-23 14:50:11 +00:00
Tim J. Robbins
b7114d4a9c Fix typo: WRDE_DOOFS -> WRDE_DOOFFS.
Noticed by:	Stoned Elipot
2004-06-30 13:55:08 +00:00
Tim J. Robbins
7937c23d49 Terminate execl()'s argument list with a null pointer instead of a
null pointer constant. (The latter may be an integer constant, which
is not correct here.)

Submitted by:	Stefan Farfeleder
2004-04-09 11:32:32 +00:00
Tim J. Robbins
2005f192e5 Use hidden names (_close, _dup2, _waitpid, etc.) where appropriate. 2003-01-07 06:07:56 +00:00
Tim J. Robbins
fe634ca75f Fix a number of bugs noticed by more extensive testing:
o Call waitpid() if an error occurs after forking the child process
   to avoid leaving zombies around.
 o Handle the WRDE_DOOFS|WRDE_APPEND combination correctly
 o Do not confuse $( substitution with $(( shell arithmetic
     (noticed by wollman)
 o Handle backslash escaping properly
 o Allow $( and ${ to be quoted
2003-01-04 06:07:44 +00:00
Tim J. Robbins
97c1c8f886 Be more consistent with "static". 2002-12-27 01:01:03 +00:00
Tim J. Robbins
faea1495bf Add an implementation of the POSIX wordexp() and wordfree() functions,
which perform shell-style word expansion on strings. This is still a
little rough around the edges.

PR:		13420
2002-12-26 14:34:18 +00:00