Commit Graph

278 Commits

Author SHA1 Message Date
Todd C. Miller
32f4b98f6b sudo frontend: silence most -Wconversion warnings. 2023-07-07 15:07:04 -06:00
Rose
45fdfa18f1 Mark functions not returning as sudo_noreturn
We also put NOTREACHED where it applies.
2023-07-01 17:40:16 -04:00
Todd C. Miller
27ea64bacd Use a "%s" format instead of using a translated string as the format. 2023-05-03 13:26:54 -06:00
Todd C. Miller
ab0f8dda31 Avoid calling isatty()/ttyname() on std{in,out,err} if not a char dev.
The user controls these fds so we should avoid calling ioctl(2) on
them unless they correspond to actual character device files.
2023-04-18 13:52:26 -06:00
Todd C. Miller
fe80c27dec Better support for "sudo -b" when running the command in a pty.
When a command is run via "sudo -b" it has no access to terminal
input.  In non-pty mode, the command runs in an orphaned process
group and reads from the controlling terminal fail with EIO.  We
cannot do the same while running in a pty but if we set stdin to a
half-closed pipe, reads from it will get EOF.  That is close enough.
2023-04-19 14:34:57 -06:00
Todd C. Miller
f0030cf30f Make struct {command,user}_details pointers const where possible. 2023-03-27 16:29:46 -06:00
Todd C. Miller
5108c279af Make user_details private to sudo.c. 2023-03-27 16:19:08 -06:00
Todd C. Miller
8d2b9a4343 Make iobufs private to exec_iolog.c. 2023-03-24 15:26:37 -06:00
Todd C. Miller
f9b1beced2 Move ptyname to struct exec_closure 2023-03-24 14:56:45 -06:00
Todd C. Miller
22776b0be6 Move pty_make_controlling() to exec_monitor.c where it is called.
We can use details->tty to access the pty follower path.
2023-03-24 14:56:13 -06:00
Todd C. Miller
7ac9ce001c Eliminate utmp_user global, just use the value in struct command details. 2023-03-24 14:44:56 -06:00
Todd C. Miller
778688d4fc Replace tty_mode global with term_raw flag in struct exec_closure.
The pty_cleanup hook needs access to the closure so add
pty_cleanup_init() to store a pointer to the closure for use
by pty_cleanup_hook().
2023-03-24 14:44:17 -06:00
Todd C. Miller
b81c5e8dac Register pty cleanup function in exec_pty(), not exec_cmnd_pty().
We want it to execute in the main sudo process, not the monitor.
2023-03-24 11:01:58 -06:00
Todd C. Miller
3303dd98c0 exec_pty.c: move foreground flag to struct exec_closure.
Also make pipeline flag private to exec_pty() and remove the unneeded
check_foreground() prototype.
2023-03-23 19:35:57 -06:00
Todd C. Miller
51cdb194b8 On resume, always sync the pty terminal settings with /dev/tty.
Changes made to the terminal settings while the command is suspended
are now reflected in the pty when the command is resumed.  This is
more consistent with the non-pty behavior and allows for the removal
of the "tty_initialized" global.  One downside to this change is
that if a terminal-based program using the pty is stopped with
SIGSTOP it may have the wrong terminal settings on resume.
However, this is no different from the non-pty case.
2023-03-23 10:39:28 -06:00
Todd C. Miller
9d55ae892f exec_pty: always copy the terminal settings from /dev/tty the pty.
Previously, we only did this when running in the foreground but
this can cause problems when running a program that reads the
terminal settings or window size in the background.  If sudo is
running in the background, the terminal settings will be updated
if it transitions to the foreground process.
Based on a suggestion from From Duncan Overbruck.
2023-03-22 12:44:41 -06:00
Todd C. Miller
98ec786b44 check_foreground: use SFD_LEADER not SFD_FOLLOWER (which was closed).
Also use SFD_LEADER for sudo_term_copy() in exec_pty() for consistency.
From Duncan Overbruck.
2023-03-22 11:21:15 -06:00
Todd C. Miller
acbe617fb4 suspend_sudo_pty: fix cut & pasto in last commit to catch SIGCONT.
Also set sa.sa_handler to SIG_DFL instead of SIG_IGN.  There is no
difference for SIGCONT but it means we can re-use sa as-is later.
2023-03-22 08:26:36 -06:00
Todd C. Miller
0fcbcaede0 Catch SIGCONT and restore terminal settings on resume from SIGSTOP.
While we cannot catch SIGSTOP, we _can_ catch SIGCONT and set
/dev/tty to raw mode when running in the foreground.  Ignore SIGCONT
in suspend_sudo_pty() so we don't call resume_terminal() twice.
2023-03-21 19:11:31 -06:00
Todd C. Miller
1772fc7b66 Only convert a signal number to a name if we are going to use it.
It is mostly used for debug logging.
2023-03-21 15:50:39 -06:00
Todd C. Miller
adb84293ab Move updating of the window size back to the main sudo process.
We can use the leader file descriptor with TIOCGWINSZ to set the
window size of the pty.  Thanks to Duncan Overbruck for the hint.
2023-03-21 15:30:54 -06:00
Todd C. Miller
19a660612f write_callback: only enable /dev/tty reader if the command is running
This fixes a hang when there is /dev/tty data in a buffer to be
flushed by the final call to del_io_events().  We do not want to
re-enable the reader when flushing the buffers as part of pty_finish().
See PR #247 for analysis of the problem and how to reproduce it.
2023-03-01 13:25:17 -07:00
Todd C. Miller
f160e5e6c6 Display error in error message if we can't restore the terminal. 2023-02-03 07:19:19 -07:00
Todd C. Miller
15b4cde692 Display an error message if unable to restore terminal settings. 2023-02-02 20:10:03 -07:00
Todd C. Miller
678d6664a9 pty_finish: only restore the terminal if sudo is the foreground process 2023-02-02 14:02:51 -07:00
Todd C. Miller
7cb23c85a1 Better background job detection when running a command in a pty.
If sudo is not the process group leader and stdin is not a tty, we
may be running as a background job via a shell script.  Start the
command in the background to avoid changing the terminal mode from
a background process.  GitHub issue #237
2023-02-02 13:55:18 -07:00
Todd C. Miller
4fb9855634 suspend_sudo_pty: stop the process group even if sudo is not the leader.
When sudo is not the process group leader, we still need to stop
sudo's process group and not just the sudo process itself.  If we
only send the signal to sudo itself, the shell will not notice if
it is not in monitor mode.  This can happen when sudo is run from
a shell script, for example.  In this case we need to signal the
shell itself.  If the process group leader is no longer present,
we must kill the command since there will be no one to resume us.
2023-02-02 13:31:16 -07:00
wanglujun
5c495b5fef debug_return_int use error 2022-12-23 10:52:01 +08:00
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