Skip to content

Commit f0b59a0

Browse files
committed
Drop priviledges and chroot after setup
Keeps the backlight file to avoid reopening after dropping priviledges. Signed-off-by: Janne Grunau <[email protected]>
1 parent 8747faf commit f0b59a0

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ libc = "0.2"
1414
input-linux = "0.6"
1515
input-linux-sys = "0.8"
1616
nix = "0.26"
17+
privdrop = "0.5.3"

src/main.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use libc::{O_RDONLY, O_RDWR, O_WRONLY};
3333
use input_linux::{uinput::UInputHandle, EventKind, Key, SynchronizeKind};
3434
use input_linux_sys::{uinput_setup, input_id, timeval, input_event};
3535
use nix::poll::{poll, PollFd, PollFlags};
36+
use privdrop;
3637

3738
const DFR_WIDTH: i32 = 2008;
3839
const DFR_HEIGHT: i32 = 64;
@@ -298,8 +299,7 @@ fn find_backlight() -> Result<PathBuf> {
298299
Err(anyhow!("No backlight device found"))
299300
}
300301

301-
fn set_backlight(path: &Path, value: u32) {
302-
let mut file = OpenOptions::new().write(true).open(path).unwrap();
302+
fn set_backlight(mut file: &File, value: u32) {
303303
file.write(format!("{}\n", value).as_bytes()).unwrap();
304304
}
305305

@@ -395,6 +395,16 @@ fn main() {
395395
]
396396
}).unwrap();
397397
uinput.dev_create().unwrap();
398+
399+
let bl_file = OpenOptions::new().write(true).open(bl_path).unwrap();
400+
401+
privdrop::PrivDrop::default()
402+
.chroot("/var/empty")
403+
.user("nobody")
404+
.group("nobody")
405+
.apply()
406+
.unwrap_or_else(|e| { panic!("Failed to drop privileges: {}", e) });
407+
398408
let mut digitizer: Option<InputDevice> = None;
399409
let mut touches = HashMap::new();
400410
let mut last_active = Instant::now();
@@ -499,7 +509,7 @@ fn main() {
499509
};
500510
if current_bl != new_bl {
501511
current_bl = new_bl;
502-
set_backlight(&bl_path, current_bl);
512+
set_backlight(&bl_file, current_bl);
503513
}
504514
}
505515
}

0 commit comments

Comments
 (0)