Skip to content

Commit d6a3dde

Browse files
committed
remove libc calls, get less bugs
1 parent 64e5e15 commit d6a3dde

File tree

4 files changed

+10
-20
lines changed

4 files changed

+10
-20
lines changed

src/common/resolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ pub(crate) fn resolve_path(command: &Path, path: &str) -> Option<PathBuf> {
210210
// replaced, or passed unchanged to the program that sudo executes.
211211

212212
path.split(':')
213-
.map(PathBuf::from)
213+
.map(Path::new)
214214
// ignore all relative paths ("", "." or "./")
215215
.filter(|path| path.is_absolute())
216216
// construct a possible executable absolute path candidate

src/sudoers/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ use std::collections::{HashMap, HashSet};
1313
use std::io;
1414
use std::path::{Path, PathBuf};
1515

16-
use crate::common::resolve::resolve_path;
16+
use crate::common::resolve::{is_valid_executable, resolve_path};
1717
use crate::defaults;
1818
use crate::log::auth_warn;
19+
use crate::system;
1920
use crate::system::interface::{GroupId, UnixGroup, UnixUser, UserId};
20-
use crate::system::{self, can_execute};
2121
use ast::*;
2222
use tokens::*;
2323

@@ -317,7 +317,7 @@ fn select_editor(settings: &Settings, trusted_env: bool) -> PathBuf {
317317
if let Some(editor) = std::env::var_os(key) {
318318
let editor = PathBuf::from(editor);
319319

320-
let editor = if can_execute(&editor) {
320+
let editor = if is_valid_executable(&editor) {
321321
editor
322322
} else if let Some(editor) = resolve_path(
323323
&editor,
@@ -337,9 +337,9 @@ fn select_editor(settings: &Settings, trusted_env: bool) -> PathBuf {
337337
// no acceptable editor found in environment, fallback on config
338338

339339
for editor in blessed_editors.split(':') {
340-
let editor = Path::new(editor);
341-
if can_execute(editor) {
342-
return editor.to_owned();
340+
let editor = PathBuf::from(editor);
341+
if is_valid_executable(&editor) {
342+
return editor;
343343
}
344344
}
345345

src/system/mod.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
#[cfg(target_os = "linux")]
33
use std::str::FromStr;
44
use std::{
5-
ffi::{c_int, c_uint, CStr, CString},
5+
ffi::{c_int, c_uint, CStr},
66
fmt, fs, io,
77
mem::MaybeUninit,
88
ops,
9-
os::unix::{self, prelude::OsStrExt},
10-
path::{Path, PathBuf},
9+
os::unix,
10+
path::PathBuf,
1111
};
1212

1313
use crate::{
@@ -41,15 +41,6 @@ pub mod wait;
4141
#[cfg(not(any(target_os = "freebsd", target_os = "linux")))]
4242
compile_error!("sudo-rs only works on Linux and FreeBSD");
4343

44-
pub(crate) fn can_execute<P: AsRef<Path>>(path: P) -> bool {
45-
let Ok(path) = CString::new(path.as_ref().as_os_str().as_bytes()) else {
46-
return false;
47-
};
48-
49-
// SAFETY: we are passing a proper pointer to access
50-
unsafe { libc::access(path.as_ptr(), libc::X_OK) == 0 }
51-
}
52-
5344
pub(crate) fn _exit(status: libc::c_int) -> ! {
5445
// SAFETY: this function is safe to call
5546
unsafe { libc::_exit(status) }

test-framework/sudo-compliance-tests/src/visudo/sudoers/editor.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ echo '{expected}' >> {LOGS_PATH}"
2727
}
2828

2929
#[test]
30-
#[ignore]
3130
fn fallback() {
3231
let expected = "configured editor was called";
3332
let editor_path = "/usr/bin/my-editor";

0 commit comments

Comments
 (0)