Skip to content

Commit acd51ef

Browse files
Charlie Tonneslansylvestre
authored andcommitted
nohup: create nohup.out with mode 0600
POSIX nohup creates the output file with permissions that block other users from reading it. We were leaving the mode at the process umask default, so on a typical system with umask 022 the file would land at 0644 and any other local user could read whatever the detached job logged. That's not great on multi-user hosts. Pass `.mode(0o600)` on the OpenOptions so newly-created `nohup.out` files start out as owner-only. Existing files keep their current permissions, which matches GNU. Closes #10021. Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
1 parent 8a271f5 commit acd51ef

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/uu/nohup/src/nohup.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustix::stdio::{dup2_stderr, dup2_stdin, dup2_stdout, stdout};
1212
use std::env;
1313
use std::fs::{File, OpenOptions};
1414
use std::io::{Error, ErrorKind, IsTerminal};
15+
use std::os::unix::fs::OpenOptionsExt;
1516
use std::os::unix::process::CommandExt;
1617
use std::path::{Path, PathBuf};
1718
use std::process;
@@ -152,7 +153,15 @@ fn find_stdout() -> UResult<File> {
152153
}
153154

154155
fn try_open_nohup_file(path: &str) -> std::io::Result<File> {
155-
let file = OpenOptions::new().create(true).append(true).open(path)?;
156+
// POSIX nohup creates the output file with mode 0600 so that other
157+
// users on a shared host can't read whatever the detached job logs.
158+
// Setting `.mode()` here only affects newly-created files; if the
159+
// file already exists its permissions are left alone.
160+
let file = OpenOptions::new()
161+
.create(true)
162+
.append(true)
163+
.mode(0o600)
164+
.open(path)?;
156165

157166
show_error!(
158167
"{}",

0 commit comments

Comments
 (0)