43 Commits

Author SHA1 Message Date
Andrew G. Morgan
7be690ed8c Improve weaver test to count zeros vs '-1' values.
This isn't fixing a known issue, but more preparing for a potential
PSX mechanism error.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2025-03-02 14:24:25 -08:00
Andrew G. Morgan
025f28ca4f Avoid thread churn deadlock with PSX rewrite.
This bug, reported by David Runge, identifies another regression
vs. libpsx-2.71:

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

A regression of the LWP threading support reintroduced an old bug
in the libpsx-2.72 rewrite, specifically this bug:

   https://github.com/golang/go/issues/42494

This present change includes x86_64, i386 and arm* (32-bit and 64-bit)
support. I've tested each of these with debian OSes, some running via QEMU.
Before releasing 2.74, I plan to extend the testing to all of the targets
supported by Debian (see https://wiki.debian.org/DebianInstaller/Qemu).

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2025-02-01 08:49:27 -08:00
Andrew G. Morgan
766527f2b9 Make make run_b219174 dependencies work.
Omitted a dependency before. Now, this works:

$ make distclean
$ cd tests
$ make run_b219174

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2024-10-26 09:03:40 -07:00
Andrew G. Morgan
12e163ac21 Rewrite libpsx to work with native linux threads.
This is more general than just working with -lpthread threads,
and addresses the following bug reports:

  https://bugzilla.kernel.org/show_bug.cgi?id=219174
  https://bugzilla.kernel.org/show_bug.cgi?id=218607
  https://bugzilla.kernel.org/show_bug.cgi?id=217476

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2024-10-25 22:58:46 -07:00
Andrew G. Morgan
f25a1b7e69 Reset the value of errno for main()
Since libcap does some error testing with a pre-main() constructor,
reset errno to zero as that constructor returns.

Problem reported by Yang Xu.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2022-01-19 06:44:58 -08:00
Andrew G. Morgan
6643c636e8 Recognize that NULL is an invalid cap_t and cap_iab_t.
This was a regresssion introduced in libcap-2.55. Fixed in libcap-2.59.
Added a cap_launch NULL test too. Comparing against NULL would cause a
SIGSEGV against these library revisions.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-09-24 10:46:24 -07:00
Andrew G. Morgan
99799844ad Don't build the tests/binaries until we want to run them
Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-09-04 13:26:56 -07:00
David Seifert
2762c2c1a8 Canonicalize build system
* Respect user's CFLAGS/CPPFLAGS/LDFLAGS
* Respect $(MAKE)
* Remove CPPFLAGS from link rules

Note: for in-tree built test binaries, where we build --static,
  we do not apply LDFLAGS: we want to limit external
  dependencies in general; and users' LDFLAGS have a strong
  tendency to conflict with --static for linking.

Work in collaboration with David Seifert (ie, he wrote most of it).

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-09-01 19:17:05 -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
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
6715a50901 Address a compiler warning with the tests/uns_test.
We're comfortable ignoring a write return code, but not all compilers
are so display a comment when the write in the uns_test fails.

This addresses:

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

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-08-25 18:48:01 -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
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
Andrew G. Morgan
fd32fac5e3 Fix cap_launch failures - error propogation.
All credit for this fix goes to Samanta Navarro. The launch error
propagation code was evidently broken previously.

Samanta also provided a proof of concept test case and we've
included that in the tests/libcap_launch_test.c.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-08-14 10:02:23 -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
06ec53d0c9 Permit compilation with -std=c89.
Tested with

  make COPTS="-O2 -std=c89" clean all test sudotest

This addresses the issue reported by Byron Stanoszek:

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

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-06-21 17:46:43 -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
572b1f8099 Validate that user namespaces require CAP_SETFCAP to map UID=0.
I found this corner case privilege escalation in December 2020.
Now that it is fixed upstream and widely deployed, add a test
so we don't regress.

[If you find 'make sutotest' fails for you, you should upgrade
your kernel.]

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-05-16 16:15:28 -07:00
Andrew G. Morgan
b08b523364 Fruit of more static analysis results
Fixes for further analysis issues from Zoltan Fridrich of Redhat.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-04-22 20:06:32 -07:00
Andrew G. Morgan
291cd980ab Add some test cases for cap_func_launcher() and callbacks
I seem to have omitted the callback test before, but now we have
cap_func_launcher() add some tests cases for it with and without
a binary to run.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2021-03-06 19:54:28 -08:00
Andrew G. Morgan
f552b8f740 Augment NOPRIV libcap mode with the sticky NO_NEW_PRIVS prctl bit.
Since I last visited securebits no privs mode, a new prctl bit
has been added (it isn't a securebit, but a parallel implementation
of something similar). So, layer that bit on top of NOPRIV mode.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2020-12-26 21:42:15 -08:00
Andrew G. Morgan
9b1c003748 Migrate building progs/tcapsh-static to sudotest
This addresses:

  https://bugzilla.kernel.org/show_bug.cgi?id=210135
  https://bugs.busybox.net/show_bug.cgi?id=13296

Some notes on expectations:

When building DYNAMIC=yes folk want to avoid depending on a
static libc.a file in their build tree. The best we can do is
to move building static test tool objects to sudotest.

  make DYNAMIC=yes clean all test

will build the progs binaries and run the tests with all dynamic
linking. It will, however, build the libcap.a etc libraries too
even though they are not used for linking.

  make DYNAMIC=no clean all test

will build the progs binaries and run the tests with all static
linking. It will, however, build the libcap.so etc libraries too
even though they are not used for linking.

  make SHARED=no clean all test

will build and link progs and test binaries statically against
libcap.a. No shared libraries (libcap.so etc) will be built.

In all cases, whether linked against or not, libcap.a is built.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2020-11-10 20:47:45 -08:00
Andrew G. Morgan
24375fb114 Support systems with no available pthreads support.
This change addresses:

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

Howto:

  make PTHREADS=no ...

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2020-10-27 20:20:47 -07:00
Andrew G. Morgan
1d03d73625 Lessen the system requirements for running make sudotest
I have one old distribution system that couldn't handle [[ ... ]]
conditions in Makefiles.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2020-10-25 08:00:24 -07:00
Andrew G. Morgan
481ca7b508 Demonstrate why libpsx is important for multithreaded C code.
I've heard a number of folk ask why one might need libpsx for anything
other than Go program linking, so this demonstrates the class of exploit
that is possible when libcap is linked with -lpthread and not -lpsx.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2020-10-24 21:00:59 -07:00
Andrew G. Morgan
a614aa669d Fix tests:sudotest for DYNAMIC=yes testing
Actually, fix the libcap_launch_test.c itself since it wasn't correctly
failing with an error exit code before.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2020-10-18 13:48:09 -07:00
Andrew G. Morgan
6b1d98b7d6 Support shared library build and linkage of -lpsx
For now, the dynamic linkage of libpsx.so* is considered alpha.
The build tree supports it and can successfully run tests but
I'm hopeful I can get some feedback from third parties that it
works as expected.

Feature request:
  https://bugzilla.kernel.org/show_bug.cgi?id=206093

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2020-10-13 21:35:55 -07:00
Andrew G. Morgan
3e43abc9c7 Be slightly more selective in what to build.
The static build of progs and tests, only needs the .a
libraries to be built.

Bug report from Thomas Petazzoni (trying to get capsh
etc to build against uClibc statically).

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2020-09-12 08:54:06 -07:00
Andrew G. Morgan
307e7f5744 Support dynamic test compilation and execution.
make DYNAMIC=yes test sudotest

works now. Thomas Petazzoni provided a patch that built
the tests this way, but I've restructured things to
make the above command line work against the uninstalled
library builds.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2020-09-07 12:53:04 -07:00
Petr Ovtchenkov
40c452c877 no more psx_test_wrap
psx_test_wrap removed in

  commit dca9b22261f4837b0c81640ca3aa5133b95e0999
  Author: Andrew G. Morgan <morgan@kernel.org>
  Date:   Thu Jul 9 19:58:26 2020 -0700

      Rewrite libpsx thread shutdown path to support musl.

      Addresses:

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

      Removed the non-wrapping libpsx macro hacks. The API surface as such
      becomes a little smaller and I now have confidence that wrapping
      pthread_create using the linker options works with Go, gcc and musl
      compilers. I feel it is stable enough to call good to delete the
      workarounds.

Signed-off-by: Petr Ovtchenkov <ptr@void-ptr.info>
Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2020-08-05 19:26:42 -07:00
Andrew G. Morgan
dca9b22261 Rewrite libpsx thread shutdown path to support musl.
Addresses:

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

Removed the non-wrapping libpsx macro hacks. The API surface as such
becomes a little smaller and I now have confidence that wrapping
pthread_create using the linker options works with Go, gcc and musl
compilers. I feel it is stable enough to call good to delete the
workarounds.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2020-07-09 22:33:54 -07:00
Andrew G. Morgan
0fe06e751a Better protect some header #define's.
Apparently some folk like to supply these defines on the compiler
command line. Protect these defines with some more macrology.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2020-06-04 21:44:09 -07:00
Andrew G. Morgan
ef485973d5 Implement cap_launch.
In threaded programs, it's a bit tricky to fork/execve a child with
capabilities different from the parent. Implement cap_launch and
friends to accomplish this.

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

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2020-02-23 16:12:12 -08:00
Andrew G. Morgan
943b011b5e A convenient IAB abstraction for inherited capability vectors.
Linux supports three flavors of inheritable capability vectors:

  - the I (inheritable set) of cap_t
  - the A (ambient) alternative to file capabilities
  - the B (bounding) vector.

The cap_iab_t collects these together into one object. I exactly equals
that of cap_t, A is what you would expect and B is "blocked" bits which
are ~cap_bound -- ie., 0 = nothing blocked.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2020-02-23 16:00:02 -08:00
Andrew G. Morgan
51ed0ec9b7 Add support for fork() in libpsx.
When we fork() we disable thread-shared semantics for
the psx_syscall()s of the child, but retain them in the
parent. This change also enhances support for unexpectedly
exiting threads (which seems to be the way Go likes to
terminate pthreads when using cgo linkage).

Enhanced licap_psx_test to fork() from created threads and
exit that thread after the forked child dies. (Ran this
10,000 times to generate confidence no races in libpsx.)

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2020-02-17 13:08:42 -08:00
Andrew G. Morgan
d45a3d4812 Ensure that gcc -std=c89 build works.
We're not using it, but it seems like a small price to
pay for having targets I'm not building regularly
continue to build.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2019-12-23 22:25:46 -08:00
Andrew G. Morgan
dada271ca4 Break out test into two parts: test or sudotest.
The latter will invoke tests with sudo. Not all builds
can support this, but we do need a convenient way
to test this stuff...

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2019-12-15 10:52:55 -08:00
Andrew G. Morgan
2b5f5635be Restructure the make files into build vs. test
Also install the Go packages if built.

Remove a default behavior of installing an inheritable bit on setcap.
I'm getting alarmed that some distributions are setting the inheritable
set to full for all users. So, I don't want to provide a vector for
a trivial exploit, and hope they are not reinventing this:

https://sites.google.com/site/fullycapable/Home/thesendmailcapabilitiesissue

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2019-12-13 17:30:23 -08:00
Andrew G. Morgan
b2b267ef1c Add support to libcap for overriding system call functions.
Note, this override only supports the system calls that
libcap uses to change kernel state associated with the
current process. This is primarily intended to permit the
user to use libpsx to force all pthreads to mirror capability
and other security relevant state.

Use a weak function definition feature of libpsx share_psx_syscall()
to transparently arrange for libcap to so force itself to use the
psx_syscall() abstraction when linked against -lpsx. This has the
effect of using linker magic to make libcap transparently observe
POSIX semantics for security state setting operations. That is, when
linked as follows:

   gcc .... -lcap -lpsx -lpthread -Wl,-wrap,pthread_create

all pthreads maintain a common security state with respect to the
libcap API.

This also adds full capability setting support to the Go package
libcap/cap via a libcap/psx package which uses cgo+libpsx syscalls
that share capabilities over all pthreads including those of the
Go runtime.

Finally, if Go supports syscall.PosixSyscall() etc. then provide
a non-psx mechanism for libcap/cap to "just work" in all Go code.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2019-12-06 23:06:47 -08:00
Andrew G. Morgan
e9f55d90e4 Implement a helper library for POSIX semantics syscalls.
Since Linux kernel supported threads are not POSIX threads
and the glibc pthread library only supports POSIX semantics
for 9 system calls, to fully support the POSIX semantics for
a process sharing its security state across all of its
threads, we've created libpsx.

This commit also includes a threading test in tests/ for
this new psx_syscall() abstraction - one that transparently
mirrors calling POSIX-needing semantics syscalls over
all running threads.

Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
2019-12-05 21:14:47 -08:00