149 Commits

Author SHA1 Message Date
Andrew G. Morgan
a5753d0291 Fix bugs in setcap.c error strings.
Multi-year bug displaying the wrong filename when failing
to set a capability on a file.

Thanks to Nikolas for reporting this:

  https://bugzilla.kernel.org/show_bug.cgi?id=220245

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2025-06-18 06:18:32 -07:00
Andrew G. Morgan
cedefd8e93 Delete a debugging printf in setcap.
Oops.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2025-03-22 09:37:42 -07:00
Leo
cf8d9ad3ec Fix reliance on transitive include.
kill(2) is declared in signal.h, however previously this header was not
included. It happened to work on glibc, where sys/wait.h includes
signal.h, this is however not required behavior in the POSIX standard:

> Inclusion of the <sys/wait.h> header may also make visible all symbols
> from <signal.h> and <sys/resource.h>.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2025-02-19 19:19:59 -08:00
Andrew G. Morgan
a1859fdc9e Fix setcap after last commit broke it.
Embarrassed. Overlooked test failure.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2024-11-22 18:36:30 -08:00
Andrew G. Morgan
80d7d006b9 Tidy up help message, and sort option processing of setcap.
Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2024-11-18 21:21:47 -08:00
Andrew G. Morgan
dc3b42a261 Work around a longstanding problem with glibc/fedora with segfaulting
https://sourceware.org/bugzilla/show_bug.cgi?id=26729

We can't rely on getpw/grid() functions working from static compiled binaries.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2024-06-22 12:28:19 -07:00
Jakub Wilk
17c5e89521 getpcaps: fix program name in help message
Signed-off-by: Jakub Wilk <jwilk@jwilk.net>
Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2024-01-29 07:19:36 -08:00
Andrew G. Morgan
6585135806 Restore c89 compaitbility for C source files.
Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2023-08-13 13:56:29 -07:00
Andrew G. Morgan
4ffb54079c Add a check for a capabability of all spaces.
Address user report of confusing behavior by adding a check to setcap
for a "<space...>" capability not meaning "-r".

Another suggestion from

  https://bugzilla.kernel.org/show_bug.cgi?id=217592

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2023-06-24 22:47:45 -07:00
Andrew G. Morgan
ac8d461a2c Make it harder to set invalid capabilities on files.
This change introduces the setcap -f argument to allow setting
of nonsense capabilities on files. But the default is to fail
when attempting to set such invalid capabilities.

This commit addresses:

  https://bugzilla.kernel.org/show_bug.cgi?id=217592

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2023-06-24 22:24:00 -07:00
Andrew G. Morgan
819f941bce Partially revive fully static binaries.
It looks like I broke the kdebug target build when I dropped fully
static building of capsh and friends. Discovered this, looking at
answering:

https://unix.stackexchange.com/questions/741532/launch-process-with-limited-capabilities-on-minimal-busybox-based-system

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2023-04-22 15:38:29 -07:00
Andrew G. Morgan
44ab72a6f9 Update documentation for all API functions.
There were a few straggler API functions in libcap and libpsx.
Also some functions that should be hidden from references outside
the library.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2023-02-19 19:43:13 -08:00
Andrew G. Morgan
ddbaa98412 Make the compare-cap binary clean up after itself.
When run via sudo, compare-cap exits with some file capabilities
left on its binary file. This is a test binary, so that's not a
big problem, however, it does mean that a 2nd run of the program
is started with, potentially, a different initial state.

This commit fixes that exit condition and addresses:

  https://bugzilla.kernel.org/show_bug.cgi?id=217018

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2023-02-09 20:13:25 -08:00
Andrew G. Morgan
f30f85c637 Modify DYNAMIC=no linking to only refer to libcap.a
There is a longstanding WONT_FIX bug:

https://sourceware.org/bugzilla/show_bug.cgi?id=12491

that has been causing capsh, when linked fully statically,
to segfault. So, for non-dynamic linking of capsh etc utilities
only link statically to libcap. This way, in tree builds can be
guaranteed to get to execute with in tree API changes. For
normal installations, DYNAMIC=yes works as before.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2022-10-15 08:29:42 -07:00
David Seifert
45bf9259a0 Use POSIX grep
* GNU grep 3.8 considers `egrep` and `fgrep` obsolescent and throws warnings:

    ./mkcapshdoc.sh > capshdoc.c.cf
    fgrep: warning: fgrep is obsolescent; using /bin/grep -F
    fgrep: warning: fgrep is obsolescent; using /bin/grep -F
    fgrep: warning: fgrep is obsolescent; using /bin/grep -F
    fgrep: warning: fgrep is obsolescent; using /bin/grep -F
    [...]

  https://lists.gnu.org/archive/html/info-gnu/2022-09/msg00001.html

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2022-09-29 06:54:24 -07:00
Andrew G. Morgan
26e3a096a4 Clean up getpcaps code.
Address some corner cases and trim down the size of the code a bit.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2022-09-04 14:36:52 -07:00
Jakub Wilk
fc804acc07 getpcaps: catch PID parsing errors.
Signed-off-by: Jakub Wilk <jwilk@jwilk.net>
Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2022-09-04 14:10:11 -07:00
Andrew G. Morgan
eb0f1df722 Prevent 'capsh --user=xxx --' from generating a bash error.
This change adds support to capsh for the --noenv argument, which
will restore pre-libcap-2.65 behavior to capsh. The change we're
making here, however, is that capsh will now set the USER and HOME
environment variables when the command line contains --user=xxx.

The issue this addresses is described here:

  https://bugzilla.kernel.org/show_bug.cgi?id=215926

This has been annoying me for long enough, and I want to clean up
the article:

  https://sites.google.com/site/fullycapable/inheriting-privilege

to not pepper "--norc" in distracting places.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2022-04-30 16:04:47 -07:00
Andrew G. Morgan
21d08b03c2 Fix syntax error in DEBUG protected setcap.c code.
Bug reported with fix from yixiangzhike.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2022-04-17 06:41:23 -07:00
Andrew G. Morgan
9c4997d659 capsh: better error handling for integer parsing.
Bug reported by meitingli:

   https://bugzilla.kernel.org/show_bug.cgi?id=214911

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-11-06 08:45:06 -07:00
Andrew G. Morgan
8e1e967bc8 setcap: clean up error handling of the ns rootid argument.
Bug reported by Artem S. Tashkinov:

   https://bugzilla.kernel.org/show_bug.cgi?id=214909

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-11-06 08:02:20 -07:00
Andrew G. Morgan
e4725c1a9e Add --quiet and cap_launch() support to capsh.
The flag --quiet causes capsh to suppress its startup check that
the linked libcap has support for all of the named capabilities
of the hosting kernel.

The cap_launch() support is via "-+" and "=+" arguments. These use
cap_launch() to fork() before exec*()ing the corresponding command
but are otherwise equivalent to "--" and "==" respectively.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-10-22 20:51:02 -07:00
Andrew G. Morgan
d07df10aaa Fix miscellaneous build and lint warnings.
Addresses the issues listed here:

   https://bugzilla.kernel.org/show_bug.cgi?id=214579

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-09-29 21:42:33 -07:00
Andrew G. Morgan
87219b72ce Make capshdoc.h stand alone compile friendly.
Signed-off-by: Andrew G. Morgan <agm@google.com>
Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-09-28 22:30:28 -07:00
Andrew G. Morgan
22569c7cfb Fix to 'make clean sudotest' reliably
Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-09-24 10:58:05 -07:00
Andrew G. Morgan
0efe94c6ec Fix typo in capsh.
Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-09-14 21:57:05 -07:00
Andrew G. Morgan
b35370f7f6 Implement --strict capsh argument.
Up to this point, capsh hides some complexity concerning raising
the CAP_SETPCAP in order to raise inheritable and drop bounding
set values. This made it harder to explain some aspects of
inheritance, and I ran into that detail writing this:

https://sites.google.com/site/fullycapable/why-didnt-that-work#h.z7rwbcazhr4r

Refactored capsh.c to clean up some buggy code, and also fix some
documentation, including reference to the --strict argument.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-09-07 13:14:54 -07:00
Andrew G. Morgan
8434c10a69 Be more systematic about POSIX.1e value group names
cap.Set's have Flag component Values
cap.IAB's have Vector component Values

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-09-07 10:47:45 -07:00
Andrew G. Morgan
8b3ffc23b6 Fixed parallel make issues
Tried make -j12 and these fixes were needed.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-09-05 20:07:48 -07:00
Andrew G. Morgan
f223de4615 Treat ENOTSUP as an expected error for getcap.
Things like /proc/* files don't support capabilities on them and
if getcap looks at them it generates a lot of errors. Treat it as
equivalent to there being no capability on the file.

This addresses

  https://bugzilla.kernel.org/show_bug.cgi?id=214317

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-09-05 19:45:05 -07:00
Andrew G. Morgan
967b3a0a95 Add --mode query support to capsh
This addresses the feature request:

  https://bugzilla.kernel.org/show_bug.cgi?id=214319

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-09-05 11:42:28 -07:00
Andrew G. Morgan
5ef14d0742 Refactor top level Makefile to reduce redundant building
Make build a bit quicker for folk that don't want to run tests.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-09-04 14:05:11 -07:00
Andrew G. Morgan
15a3d49bf1 Move $(LDFLAGS) earlier in build command lines.
As explained (thanks David Seifert) there are some LDFLAGS that
need to precede actual linked libraries. For example, -Wl,--as-needed.
Given this, I've tried it and it appears to work for the default
build cases as captured in 'make distcheck'.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-08-31 19:05:59 -07:00
Andrew G. Morgan
f5ae31ebd3 Be more consistent with make variables.
Noticed that we weren't applying the same amount of flag discipline
to local BUILD_* tool rules. Fixing that, I see we've been carrying
a source code issue in libcap/_makenames.c for a while. (FIXED).

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-08-30 20:02:22 -07:00
Andrew G. Morgan
61b2fcc451 Make sudotest more robust against untestable environments
I'm setting up some testing environments and they are not all
created equal.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-08-28 16:50:35 -07:00
Andrew G. Morgan
552db8f411 More fixes for static analysis issues.
Further observations from Zoltan Fridrich's static analysis of libcap.
This commit also includes a fix for something I broke with the last
round of "fixing", and a test to make sure I don't make that mistake
again.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-08-27 10:26:59 -07:00
Andrew G. Morgan
c90b5debdf Fix some static analysis results.
This series of issues was found by Zoltan Fridrich.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-08-26 20:24:47 -07:00
Andrew G. Morgan
935ab8f7cd Support overriding choice of 'sudo'.
Use something like:

  make SUDO=my_sudo sudotest

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-08-25 19:48:12 -07:00
Andrew G. Morgan
04f903f915 Add some more information to help, and contextual error messages.
This was inspired by a feature Debian has been patching orginally
credited to Zhi Li.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-08-25 19:38:13 -07:00
Andrew G. Morgan
be65c32ded Make 'progs/getpcaps --iab' act like 'go/captree' in output format
The combined options 'getpcaps --iab --verbose' will show everything
in detail (even the boring stuff).

Also used this exercise to test the libcap changes for iab comparisons.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-08-23 20:09:53 -07:00
Andrew G. Morgan
5647374b33 Revert most of the LDFLAGS -> LDSTATIC change.
The intention was to force --static linking in only one corner case,
so be more explicit about that one, and revert the build behavior
in the others.

Reason for doing this was feedback from Arnout Vandecappelle in:

  https://bugzilla.kernel.org/show_bug.cgi?id=214023#c16

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-08-23 19:47:38 -07:00
Andrew G. Morgan
4f45bcc835 Add cap_iab_{compare,get_pid} functions to libcap; --iab to getpcaps.
This brings libcap back to parity with the Go 'cap' package. We
provide a CAP_IAB_DIFFERS(result, vector) macro to evaluate the result
of cap_iab_compare().

Extend the getpcaps arguments to include --iab. This causes the utility
to explore the IAB tuple for the specified process. When used, this
outputs a text representation in a similar format to that of the
'captree' (Go) utility.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-08-22 21:13:56 -07:00
Andrew G. Morgan
d21a561ddb Drop non-standard IPATH in favor of CPPFLAGS.
This also required locally augmenting CFLAGS with -fPIC in the
Makefile's that required it.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-08-19 20:49:39 -07:00
Samanta Navarro
e721aff543 Fix typos.
Typos found with codespell

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-08-14 11:03:27 -07:00
Andrew G. Morgan
d5daba542a Support distributions that build libcap with aggressive link options.
Discussion of one such setup in this bug (reported by David Runge):

  https://bugzilla.kernel.org/show_bug.cgi?id=214023

Work around the failure to run ./pam_cap.so in these cases with
some more Makefile magic, and adjust test building with these
flags so it works in DYNAMIC=yes|no and SHARED=yes|no cases.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-08-13 20:43:14 -07:00
Andrew G. Morgan
f73a3691af When --suggest'ing also compare against capability names
Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-07-02 21:17:27 -07:00
Andrew G. Morgan
69f7ddbb12 Provide a '--current' command line argument for capsh.
This is equivalent to 'capsh --print|fgrep Current'. I've been using
that combination a lot in the write-ups on the libcap website
(https://sites.google.com/site/fullycapable/) and so it struck me
that capsh probably should support it natively.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-07-02 19:51:51 -07:00
Andrew G. Morgan
2efece0bb4 Clean up some compilation warnings
Also amend .gitignore to ignore uns_test binary in the progs directory.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-06-20 13:19:40 -07:00
Andrew G. Morgan
2c99ecd7ee Add kernel test to quicktest.sh
This resolves:

  https://bugzilla.kernel.org/show_bug.cgi?id=212737

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-06-19 17:36:46 -07:00
Andrew G. Morgan
fc82332156 Make capsh an installed binary again
Bug report from Jan Palus:

  https://bugzilla.kernel.org/show_bug.cgi?id=213261

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-05-28 13:41:17 -07:00