Commit Graph

274 Commits

Author SHA1 Message Date
Todd C. Miller
4f3786f70f visudo: do not create a new file if the user made no changes
This prevents visudo from creating a new zero-length sudoers file
if the user exited the editor without making any changes.  Files
created via a @include directive are preserved, even if empty, to
avoid a parse error.  GitHub issue #294.
2023-07-27 11:23:25 -06:00
Rose
5d758264ab Give every printf-like function restrict qualifiers
The format value has to be a string literal, every time.

Otherwise, you are not using these functions correctly. To reinforce this fact, I putrestrict over every non-contrib example of this I could find.
2023-07-07 20:23:20 -04:00
Todd C. Miller
a38b714667 sudoers plugin: silence most -Wconversion warnings. 2023-07-07 15:07:04 -06:00
Todd C. Miller
04c7e910ef Fix up indentation. 2023-07-01 16:03:33 -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
Rose
22079c3072 Avoid compiler casting warnings Part 2
This saves instructions that are related to casting as well as compiler warnings.
2023-06-29 13:10:27 -04:00
Rose
e54ba33ea0 Avoid compiler casting warnings by assigning to the same type where possible
This saves instructions that are related to casting as well as compiler warnings.
2023-06-28 17:25:26 -04:00
Todd C. Miller
2392ee9d7d run_command: back out changes to run editor in its own process group.
It unnecessarily complicates things to work around bugs in an OS
almost no one runs.
2023-06-04 19:11:48 -06:00
Todd C. Miller
d633d82f22 Fix typos and update excluded/ignored codespell lists. 2023-05-25 12:53:27 -06:00
Todd C. Miller
0986a8846e run_command: check that ttyfd is not -1 before using it 2023-05-19 13:38:48 -06:00
Todd C. Miller
4d417b46d9 Work around a macOS a kernel bug where tcsetpgrp() does not restart.
I reported this bug to Apple over 12 years ago.
2023-05-11 18:22:06 -06:00
Todd C. Miller
be20e1592f run_command: run editor in foreground if visudo is the foreground process
The command is now always run in its own process group.  If visudo
is run in the foreground, the command is run in the foreground too.
Otherwise, run the command in the background.  There is a race
between the tcsetpgrp() call in the parent and the execve() in the
child.  If we lose the race and the command needs the controlling
terminal, it will be stopped with SIGTTOU or SIGTTIN, which the
waitpid() loop will handle.
2023-05-11 18:20:50 -06:00
Todd C. Miller
7e28e60b56 Accept carriage return for EOL in addition to newline.
Since visudo doesn't alter the terminal settings it is possible for
the terminal to have the ONLCR bit set in the output control flags.
In that case, we will get a CR, not a NL when the user presses
enter/return.  One way this can happen is if visudo is run in the
background from a shell that supports line editing and the editor
restores the (cbreak-style) terminal mode when it finishes.
2023-05-11 18:12:54 -06:00
Todd C. Miller
bde363e060 Rename parser_conf -> sudoers_conf in all but the parser itself. 2023-05-09 07:29:06 -06:00
Todd C. Miller
8cbd5462a6 Move sudoers search path to struct sudoers_parser_config.
That way we can avoid passing it to init_parser() directly.
We still need sudoers_search_path to be shared between the lexer
and the parser.
2023-05-08 17:04:38 -06:00
Todd C. Miller
9d7c30c5a8 Add struct sudoers_parser_config and pass it to init_parser().
This struct contains parser configuration such as the sudoers file
uid/gid/mode and parse flags such as verbose, strict and recovery.
2023-05-08 17:03:31 -06:00
Todd C. Miller
bbbaea5b2f Rename init_parser_ext() to init_parser() and remove old wrapper.
There was only one consumer of the init_parser() wrapper now that
reset_parser() has been introduced.
2023-05-02 13:29:49 -06:00
Todd C. Miller
f553ddc430 Make sudoers_file private to policy.c and visudo.c.
We just need a way for the policy (and visudo) to override the
default sudoers path.  This adds a getter to be used in file.c when
sudoers is first opened.
2023-05-02 10:47:47 -06:00
Todd C. Miller
5446b009e3 Support adminconfdir for relative include paths in sudoers. 2023-05-02 10:47:11 -06:00
Todd C. Miller
7a6ac5d26e Track the destination sudoers path for each parsed file.
When adminconfdir is enabled, the destination pathh may be different
from the path we opened.  We always store an edited file in the
adminconfdir (if enabled).  This makes it possible to use visudo
when /etc/sudoers is located on a read-only file system.
2023-05-02 10:45:56 -06:00
Todd C. Miller
9ad6dfa371 Substitute for _PATH_SUDO* variables in pathnames.h.
Previously these were hard-coded with Makefile overrides.
2023-02-08 16:47:18 -07:00
Todd C. Miller
f17b35471f Support sudoers_file being a colon-separated path of files.
The first file found is used.
2023-05-02 10:37:38 -06:00
Todd C. Miller
a17491972b Quiet compiler warnings on systems where pid_t is not an int.
Historically, pid_t was a long on some 32-bit systems like Solaris.
2023-03-22 14:48:49 -06:00
Todd C. Miller
775059156f Silence "used uninitialized" false positives with older gcc versions. 2023-03-22 13:52:43 -06:00
Todd C. Miller
c64993a0b3 visudo: restore controlling terminal after running the editor.
Otherwise, visudo will get SIGTTOU if it tries to write to the
terminal after the editor finishes.  Also avoid races by setting
the process group ID in both the parent and child, and grant the
controlling terminal in the parent, not the child.
2023-03-21 09:22:53 -06:00
Todd C. Miller
8049e4e32f Replace sudoers_warnings with sudoers_verbose.
This is now an int, with values > 1 reserved for visudo.
2023-03-20 18:27:27 -06:00
Todd C. Miller
87ce692468 Fix potential double free for rules that include a CHROOT= option.
If a rule with a CHROOT= option matches the user, host and runas,
the user_cmnd variable could be freed twice.
2023-02-21 20:01:13 -07:00
Todd C. Miller
1641c30ed6 visudo: quiet a compiler warning on Solaris 10.
Also explicitly close /dev/tty fd instead of relying on closefrom()
in case the fd ends up being a value 0-2.
2023-02-22 10:55:27 -07:00
Todd C. Miller
ba7b126451 Check tcsetpgrp() return value. 2023-02-21 16:34:12 -07:00
Todd C. Miller
0339337103 Run the editor in its own process group.
This fixes suspending the editor on GNU Hurd which doesn't seem to
have proper process group signal handling.
2023-02-21 16:14:14 -07:00
Todd C. Miller
0274a4f3b4 sudoedit: do not permit editor arguments to include "--" (CVE-2023-22809)
We use "--" to separate the editor and arguments from the files to edit.
If the editor arguments include "--", sudo can be tricked into allowing
the user to edit a file not permitted by the security policy.
Thanks to Matthieu Barjole and Victor Cutillas of Synacktiv
(https://synacktiv.com) for finding this bug.
2023-01-12 15:55:27 -07:00
Todd C. Miller
e707ffe58b Place C23 attributes before keywords in function declarations.
In practice this means we must use "sudo_noreturn static foo(void)"
instead of "static sudo_noreturn foo(void)".
2022-12-01 12:54:53 -07:00
modric
26cf125fb9 check_syntax(): Remove duplicate calls to init_defaults() 2022-11-24 09:42:05 +08:00
Todd C. Miller
f066ff9e01 Eliminate a few harmless dead stores.
Quiets warnings from Infer.
2022-11-22 11:18:24 -07:00
Todd C. Miller
fa952bfbb7 Add -I flag to disable editing include files unless there is an error.
This can be used when you only want to edit a single sudoers file
unless there is a pre-existing syntax error.
2022-10-04 16:11:45 -06:00
Todd C. Miller
304726a215 Move gcc-style __attribute__ macros to config.h.in
Renamed __malloc -> sudo_malloclike, __printflike -> sudo_printflike,
__printf0like -> sudo_printf0like.
Add sudo_noreturn instead of __attribute__((__noreturn__)).
We do not use stdnoreturn.h since it has been deprecated in C23
in favor of the [[noreturn]] attribute.
2022-09-07 07:48:31 -06:00
Todd C. Miller
89a40741b4 Add __printf0like to visudo_track_error(). 2022-09-07 07:25:31 -06:00
Carlo Teubner
2b5dcf255d visudo.c: add nvim (Neovim) to lineno_editor list
Neovim supports it: https://neovim.io/doc/user/starting.html#-+
2022-07-31 17:20:50 +01:00
Todd C. Miller
fba81260ad If update_defaults() fails, treat it as a parse error. 2022-07-08 14:01:15 -06:00
Todd C. Miller
13672f28df Make sudo pass -Wwrite-strings 2022-06-28 16:33:15 -06:00
Todd C. Miller
ef4ce5c08a Looser owner/permission checks for an uninstalled sudoers file.
We don't check the owner or permissions on a sudoers file that is
specified as an argument to visudo by default.  However, the owner
and mode of files included via @includedir were still checked.  This
commit makes the owner and permissions checks for filed included
via @includedir follow the same as for the original sudoers file.
2022-03-11 10:44:17 -07:00
Todd C. Miller
d7ddff2a31 Add a hook for sudoers parse errors (including defaults and aliases).
The hook can be used to log parser errors (sudoers module) or keep
track of which files have an error (visudo).
Previously, we only kept track of a single parse error.
2022-03-10 13:30:56 -07:00
Todd C. Miller
7d3f9293c6 Preserve the column and error message when there is a syntax error.
This information is now included in the error mail sent to root.
2022-03-06 18:54:30 -07:00
Todd C. Miller
55d17eaa34 install_sudoers: fix return value when there is no temp file to install
This can happen when no changes were made.  Also preserve the edited
temp file on error if we are unable to move it into place.
2021-11-09 16:09:48 -07:00
Todd C. Miller
288bb82543 Rename {check,set}_perms variable to {check,set}_mode.
Avoids a name clash with the set_perms() function.
2021-11-09 13:41:27 -07:00
Todd C. Miller
cdd41af66f visudo: add -O and -P options to check/set owner and permissions.
This can be used in conjunction with the -c option to check that
the sudoers file ownership and permissions are correct.
Bug #1007
2021-11-06 12:37:13 -06:00
Todd C. Miller
d15e117c2e find_editor: remove the env_error argument
There is no case where we should fail to find an editor just because
the values of EDITOR, VISUAL and SUDO_EDITOR are unavailable.  Both
sudoedit and the "env_editor" sudoers setting are documented as
falling back on the hard-coded list of editors in the "editors"
sudoers setting.  Bug #1000
2021-09-29 10:25:19 -06:00
Todd C. Miller
8b009f62eb Plug memory leak in error path when sudoers cannot be opened. 2021-07-29 15:26:04 -06:00
Todd C. Miller
2db9e64214 Move reference-counted string code from sudoers to libsudo_util.
It will be used by sudo_logsrvd too.
2021-04-06 14:44:19 -06:00
Todd C. Miller
5a85543c16 Move alias checking code out of visudo.c and into check_aliases.c. 2021-02-23 18:42:37 -07:00