Commit Graph

250 Commits

Author SHA1 Message Date
Todd C. Miller
f87fac3b55 Don't kill the parent process group on suspend if it is not sudo's pid.
If sudo is not the process group leader we must only send the suspend
signal to sudo itself.  When sudo is run via a shell script, it
usually has the same process group as the shell script interpreter.
We do not want to suspend the script itself when the command run
by sudo is suspended.
2022-11-07 14:51:43 -07:00
Todd C. Miller
36742deec3 Fix handling of signal forwarding when running commands in a script.
We need to forward signals from a process in the same pgrp if the
pgrp leader is not either sudo or the command itself.
2022-11-07 14:51:41 -07:00
Todd C. Miller
87b7209ebb Add support for logging stdin/stdout/stderr in the non-pty exec path.
If we are logging I/O but not terminal input/output (either because
no terminal is present or because that is what the plugin requested),
the non-pty exec path is now taken.
2022-09-27 13:46:55 -06:00
Todd C. Miller
803b4939be Move exec code to call into I/O log plugin to exec_iolog.c.
This will be shared with exec_nopty.c in the future to log
stdin/stdout/stderr without running the command in a pty.
Both exec_pty.c and exec_nopty.c now use the same closure.
2022-09-27 13:35:45 -06:00
Todd C. Miller
964bcfa2dd Make read and write events persistent and disable as needed.
For the read callback, disable reader when the buffer is full.
For the write callback, disable writer when the buffer is consumed.
2022-06-07 12:40:00 -06:00
Todd C. Miller
c2a131714a Reinstall the event handler if we get EAGAIN from read/write callback.
The read and write events do not set SUDO_EV_PERSIST so we need to
explicitly re-enable the event if there is still data to be read.
Bug #963.
2022-06-06 19:42:07 -06:00
Todd C. Miller
b203753013 Pass the WUNTRACED flag to waitpid() even if __WALL is present.
Otherwise, we won't get the wait status of a suspended command
that is not being traced.
2022-05-19 19:48:24 -06:00
Todd C. Miller
0ea431e392 Move code to suspend sudo when no pty is in use to separate file.
Use this in test_ptrace.c to be able to suspend just like sudo does.
2022-05-18 07:29:55 -06:00
Todd C. Miller
a52e3776f0 Fix suspending a sudo-run shell in ptrace intercept mode with no pty.
When ptracing a process, we receive the signal-delivery-stop signal
before the group-stop signal.  If sudo is running the command in
the same terminal, we need to wait until the stop signal is actually
delivered to the command before we can suspend sudo itself.  If we
suspend sudo before receiving the group-stop, the command will be
restarted with PTRACE_LISTEN too late and will miss the SIGCONT
from sudo.
2022-05-17 14:26:03 -06:00
Todd C. Miller
4ab6a87b96 Initialize intercept_allow_setid to true if we use ptrace(2) and seccomp(2). 2022-05-04 13:32:28 -06:00
Todd C. Miller
e84fdd99fd If the process is already being traced, just resume it and clear flags.
This makes it possible to run sudo in ptrace intercept mode from within
a shell (or other process) that is already being traced by sudo.
2022-05-03 13:34:40 -06:00
Todd C. Miller
3ee8bcefb0 Kill the command if intercept_setup() or ptrace_seize() fail. 2022-05-03 09:25:58 -06:00
Todd C. Miller
8e375445fb Check the policy for ptrace-based intercept mode. 2022-04-29 13:08:59 -06:00
Todd C. Miller
01733a5214 Add scaffolding for ptrace-based intercept mode. 2022-04-29 12:35:31 -06:00
Todd C. Miller
22866f2423 Handle multiple child processes in the SIGCHLD handler.
This is required by the uncoming ptrace intercept code.
2022-04-29 08:02:57 -06:00
Todd C. Miller
46edc4e198 Stop using the WCONTINUED flag with waitpid(2).
We don't use it for anything other than a debug message and it will
cause problems when intercept mode starts using ptrace(2).
2022-04-29 08:02:57 -06:00
Todd C. Miller
de47380350 Block SIGCHLD when forking the mailer.
Otherwise, it may be picked up by the signal handler instead of our
waitpid(2) call.
Don't warn if waitpid() returns 0 in a SIGCHLD handler.
2022-03-14 13:54:12 -06:00
Todd C. Miller
a8c4d9800b Add intercept_cleanup() to free the closure used by intercept_accept_cb(). 2021-09-20 08:50:42 -06:00
Todd C. Miller
ba171724f7 Rename log_children -> log_subcmds 2021-08-26 16:36:41 -06:00
Todd C. Miller
c465d8971d Change intercept IPC to use a localhost socket instead of inherited fd.
This allows intercept mode to work with shells that close all open
fds upon startup.  The ctor in sudo_intercept.so requests the port
number and secret over the socket inherited from the parent then
closes it.  For each policy request, a TCP connection is made to
the sudo parent process to perform the policy check.  Child processes
re-use the TCP socket to request the port number and secret just like
the initial process started by sudo does.
2021-08-25 14:24:36 -06:00
Todd C. Miller
a55b54329e Add missing stdint.h and sudo_rand.h includes.
Needed for arc4random() and uin64_t.
2021-08-13 09:25:01 -06:00
Todd C. Miller
eaf03a382b Pass a secret value to sudo_intercept.so and verify after policy check.
The goal is to make it harder for someone to have a fake policy checker.
This will not stop a determined adversary since the secret is present
in the address space of the running process.
2021-08-13 09:10:44 -06:00
Todd C. Miller
13b89e9103 Make the log_children option only log and not check policy. 2021-08-09 15:50:25 -06:00
Todd C. Miller
a556b373c9 Allocate a socketpair to communicate with sudo_intercept.so over.
This is used for the intercept and log_children options.
2021-08-09 15:50:25 -06:00
Todd C. Miller
3147bbeb24 Don't set the command status in the closure when the command is suspended.
This should only be set for signals that terminate the process.
Fixes a bug where the sudo front-end could call the plugin close
function with a non-terminal signal argument.
2021-05-10 13:42:06 -06:00
Todd C. Miller
ece5adc662 Add struct sudo_cred to hold the invoking or runas user credentials.
We can use this when we need to pass around credential info instead
of the user_details and command_details structs.
2021-01-06 13:01:09 -07:00
Todd C. Miller
24d812d037 Pass command_info[] to audit plugin on I/O log plugin reject or error.
The audit plugin should cope with a NULL command_info but there's no
reason not to pass the info when we have it.
2020-11-17 13:15:15 -07:00
Todd C. Miller
961a4afe67 Fix some warnings from pvs-studio 2020-08-12 13:45:09 -06:00
Todd C. Miller
03ad96e445 Use the fallthrough attribute instead of /* FALLTHROUGH */ comments. 2020-08-01 13:10:50 -06:00
Todd C. Miller
8bb5aedacf Replace master/slave in code with leader/follower. 2020-06-15 16:21:32 -06:00
Todd C. Miller
a380709215 If event loop fails due to ENXIO, remove /dev/tty events and recover.
This fixes an issue on Solaris 11.4 (and probably others) with "sudo
reboot" when I/O logging is enabled.  Previously, sudo would kill
the command if it was still running after the event loop terminated,
leaving the system in a half-dead state.
2020-06-02 08:59:38 -06:00
Todd C. Miller
592eb7ab49 Don't try to suspend sudo if the user's tty has gone away.
Fixes a problem on Solaris 11.4 (and possibly others) where sudo
continually tries to put itself in the background after the user's
terminal has been revoked.
2020-06-01 14:19:50 -06:00
Todd C. Miller
de31e6d53b Back out WIP code that was mistakenly committed. 2020-06-01 14:04:15 -06:00
Todd C. Miller
ad70fb4f1e On Solaris 11.4 the openpty(3) prototype lives in termios.h. 2020-06-01 12:59:29 -06:00
Todd C. Miller
446ae3f507 Include string.h unconditionally and only use strings.h for strn?casecmp()
In the pre-POSIX days BSD had strings.h, not string.h.
Now strings.h is only used for non-ANSI string functions.
2020-05-18 07:59:24 -06:00
Todd C. Miller
0cf2e09e0c Apply spelling fixes.
Fixes from PR #30 (ka7) and Bug #925 (fossies.org codespell)
2020-05-06 09:27:43 -06:00
Todd C. Miller
3221c8b4d8 Don't kill the command just because the loop exited unexpectedly.
We currently have no good way to distinguish between an error
executing the command and an error while the command is running.

In the future, we should have additional status codes so we
can tell what type of condition caused the loop to exit.

For now, only kill the command if cstat is left uninitialized.
2020-03-31 08:49:30 -06:00
Todd C. Miller
ac61b5655d Use EXIT_SUCCESS and EXIT_FAILURE more consistently. 2020-02-08 12:43:00 -07:00
Todd C. Miller
22105009d8 Define a new plugin type that receives accept and reject messages.
This can be used to implement logging-only plugins.
The plugin functions now take an errstr argument that can be used
to return an error string to be logged on failure or error.
2020-01-30 13:25:34 -07:00
Todd C. Miller
486ee2b71f debug_decl and debug_decl_vars now require a semicolon at the end. 2019-12-22 08:48:16 -07:00
Todd C. Miller
82237194dd Add support for logging to the log server 2019-11-15 13:41:51 -07:00
Todd C. Miller
5793023ffd Add a plugin interface to sudo main event loop. 2019-11-15 13:36:01 -07:00
Todd C. Miller
352212c7bf Open all pipes using pipe2() with O_CLOEXEC.
We no longer depend on calling closefrom() before exec.
2019-11-02 10:52:34 -06:00
Todd C. Miller
112dff276a Fix restoring the file context of the user's tty with SELinux.
Also fix broken tty labeling when running a command in a pty.
Includes a fix for a typo introduced in the last change set.
2019-09-27 15:32:49 -06:00
Todd C. Miller
1e1ef61902 Add SPDX-License-Identifier to files. 2019-04-29 07:21:51 -06:00
Todd C. Miller
985600e7f0 Minor snprintf() usage tweaks:
1) don't assume snprintf() returns -1 on error, check for <0
2) when comparing return value of sizeof(foo), cast the sizeof, not the len
3) cast return value to void in cases where snprintf cannot fail
2019-01-20 07:49:48 -07:00
Todd C. Miller
64d08d8bf2 Update copyright year 2019-01-03 11:09:00 -07:00
Todd C. Miller
b2f7983c84 Fix setting of utmp entry when running command in a pty.
Regression introduced in sudo 1.8.22.
2019-01-02 07:39:33 -07:00
Todd C. Miller
6c3d20cb41 Convert PVS-Studio comment to ANSI C. 2018-10-26 08:39:09 -06:00
Todd C. Miller
64e5d34c57 Add comments in .c files so PVS-Studio will check them. 2018-10-21 08:46:05 -06:00