team-ljyzipelo

공개

사용하지 않는 sss 계정 소스만 허용함

작성자
시로사키 하나
작성 시각
커밋
4638d9faf4a2c220c95bca76b06bddc61ef6d3fa
부모
0d51c35
이 시점의 파일 보기

변경된 파일 3개

+118-5
수정experiments/rust-archive-worker/src/filesystem.rs+34-4
@@ -297,7 +297,29 @@ mod platform {
297297 }
298298 }
299299
300삭제: // files와 systemd만 열거하며 원격 계정 소스와 임의의 NSS 제어 규칙은 거부함.
300추가: // 없는 경로만 허용하며 중간 심볼릭 링크와 확인 오류는 거부함.
301추가: fn path_is_absent(path: &Path) -> bool {
302추가: let mut prefix = std::path::PathBuf::new();
303추가: for component in path.components() {
304추가: prefix.push(component.as_os_str());
305추가: match std::fs::symlink_metadata(&prefix) {
306추가: Ok(metadata) if metadata.is_dir() => {}
307추가: Ok(_) => return false,
308추가: Err(error) if error.kind() == std::io::ErrorKind::NotFound => return true,
309추가: Err(_) => return false,
310추가: }
311추가: }
312추가: false
313추가: }
314추가:
315추가: fn sss_is_unused(socket: &Path, cache: &Path) -> bool {
316추가: path_is_absent(socket)
317추가: && ["passwd", "group", "initgr"]
318추가: .iter()
319추가: .all(|name| path_is_absent(&cache.join(name)))
320추가: }
321추가:
322추가: // sss는 소켓과 캐시가 모두 없을 때만 빈 소스로 취급함.
301323 fn primary_group_is_private(gid: u32) -> bool {
302324 let Ok(policy) = std::fs::read_to_string("/etc/nsswitch.conf") else {
303325 return false;
@@ -310,9 +332,17 @@ mod platform {
310332 let Some(source) = sources.next() else {
311333 return false;
312334 };
335추가: let source: Vec<_> = source.split_whitespace().collect();
313336 if sources.next().is_some()
314삭제: || !(source.split_whitespace().eq(["files"])
315삭제: || source.split_whitespace().eq(["files", "systemd"]))
337추가: || !matches!(
338추가: source.as_slice(),
339추가: ["files"] | ["files", "systemd"] | ["files", "sss"] | ["files", "systemd", "sss"]
340추가: )
341추가: || (source.contains(&"sss")
342추가: && !sss_is_unused(
343추가: Path::new("/var/lib/sss/pipes/nss"),
344추가: Path::new("/var/lib/sss/mc"),
345추가: ))
316346 {
317347 return false;
318348 }
@@ -341,7 +371,7 @@ mod platform {
341371 }
342372 }
343373 found_current
344삭제: && (!source.split_whitespace().any(|source| source == "systemd")
374추가: && (!source.contains(&"systemd")
345375 || systemd_primary_group_is_private(gid, current_uid))
346376 }
347377
수정tests/rust-archive-worker/private_group.rs+56-1
@@ -51,7 +51,7 @@ fn run_case(case: &str, safe: bool, extraction: bool) {
5151 fs::remove_file(&passwd_path).unwrap();
5252 }
5353 let nss = match case {
54삭제: "remote-accounts" => "passwd: files sss\n",
54추가: "remote-accounts" | "sss-files-only" => "passwd: files sss\n",
5555 "systemd-personal"
5656 | "systemd-dynamic-shared"
5757 | "systemd-homed-shared"
@@ -67,6 +67,7 @@ fn run_case(case: &str, safe: bool, extraction: bool) {
6767 "ldap-accounts" => "passwd: files ldap\n",
6868 "winbind-accounts" => "passwd: files winbind\n",
6969 "systemd-remote" => "passwd: files systemd sss\n",
70추가: _ if case.starts_with("sss-") => "passwd: files systemd sss\n",
7071 "unknown-accounts" => "passwd: files unknown\n",
7172 "short-circuit-policy" => "passwd: files [SUCCESS=return] systemd\n",
7273 "duplicate-policy" => "passwd: files\npasswd: files\n",
@@ -78,6 +79,39 @@ fn run_case(case: &str, safe: bool, extraction: bool) {
7879 if case == "nss-unavailable" {
7980 fs::remove_file(&nss_path).unwrap();
8081 }
82추가: let sss = scratch.0.join("sss");
83추가: fs::create_dir_all(sss.join("pipes")).unwrap();
84추가: fs::create_dir_all(sss.join("mc")).unwrap();
85추가: let _socket = match case {
86추가: "sss-socket" | "remote-accounts" | "systemd-remote" => {
87추가: use std::os::fd::AsRawFd;
88추가: let pipes = fs::File::open(sss.join("pipes")).unwrap();
89추가: let path = format!("/proc/self/fd/{}/nss", pipes.as_raw_fd());
90추가: Some(std::os::unix::net::UnixListener::bind(path).unwrap())
91추가: }
92추가: _ => None,
93추가: };
94추가: match case {
95추가: "sss-passwd-cache" => fs::write(sss.join("mc/passwd"), b"").unwrap(),
96추가: "sss-group-cache" => fs::write(sss.join("mc/group"), b"").unwrap(),
97추가: "sss-initgr-cache" => fs::write(sss.join("mc/initgr"), b"").unwrap(),
98추가: "sss-dangling-socket" => {
99추가: std::os::unix::fs::symlink("missing", sss.join("pipes/nss")).unwrap();
100추가: }
101추가: "sss-dangling-cache" => {
102추가: std::os::unix::fs::symlink("missing", sss.join("mc/passwd")).unwrap();
103추가: }
104추가: "sss-cache-symlink" => {
105추가: fs::remove_dir(sss.join("mc")).unwrap();
106추가: std::os::unix::fs::symlink("missing", sss.join("mc")).unwrap();
107추가: }
108추가: "sss-invalid-parent" => {
109추가: fs::remove_dir(sss.join("mc")).unwrap();
110추가: fs::write(sss.join("mc"), b"").unwrap();
111추가: }
112추가: "sss-missing-directories" => fs::remove_dir_all(&sss).unwrap(),
113추가: _ => {}
114추가: }
81115 let library = scratch.0.join("private-group.so");
82116 assert!(
83117 Command::new("cc")
@@ -103,6 +137,7 @@ fn run_case(case: &str, safe: bool, extraction: bool) {
103137 .env("ZIPELO_GROUP_CASE", case)
104138 .env("ZIPELO_GROUP_PASSWD", passwd_path)
105139 .env("ZIPELO_GROUP_NSS", nss_path)
140추가: .env("ZIPELO_GROUP_SSS", sss)
106141 .env("ZIPELO_GROUP_OUTPUT", &output)
107142 .env("ZIPELO_GROUP_SAFE", if safe { "1" } else { "0" })
108143 .env("ZIPELO_GROUP_EXTRACT", if extraction { "1" } else { "0" })
@@ -218,3 +253,23 @@ fn private_group_rejects_consumed_systemd_record_on_erange() {
218253 check_extraction(case, false);
219254 }
220255}
256추가:
257추가: #[test]
258추가: fn private_group_accepts_sss_only_without_socket_and_caches() {
259추가: for case in ["sss-empty", "sss-files-only", "sss-missing-directories"] {
260추가: check_extraction(case, true);
261추가: }
262추가: for case in [
263추가: "sss-socket",
264추가: "sss-passwd-cache",
265추가: "sss-group-cache",
266추가: "sss-initgr-cache",
267추가: "sss-dangling-socket",
268추가: "sss-dangling-cache",
269추가: "sss-cache-symlink",
270추가: "sss-invalid-parent",
271추가: "sss-probe-error",
272추가: ] {
273추가: check_extraction(case, false);
274추가: }
275추가: }
수정tests/rust-archive-worker/private_group_nss.c+28-0
@@ -121,3 +121,31 @@ int _nss_systemd_getpwent_r(struct passwd *account, char *buffer, size_t length,
121121 };
122122 return 1;
123123}
124추가:
125추가: #include <limits.h>
126추가: #include <stdio.h>
127추가: #include <sys/stat.h>
128추가:
129추가: int statx(int fd, const char *path, int flags, unsigned int mask,
130추가: struct statx *info) {
131추가: int (*original)(int, const char *, int, unsigned int, struct statx *) =
132추가: dlsym(RTLD_NEXT, "statx");
133추가: const char *prefix = "/var/lib/sss";
134추가: const size_t size = strlen(prefix);
135추가: if (!strncmp(path, prefix, size) && (!path[size] || path[size] == '/')) {
136추가: const char *base = getenv("ZIPELO_GROUP_SSS");
137추가: const char *phase = getenv("ZIPELO_GROUP_CASE");
138추가: if (!base || (phase && !strcmp(phase, "sss-probe-error"))) {
139추가: errno = EACCES;
140추가: return -1;
141추가: }
142추가: char redirected[PATH_MAX];
143추가: int n = snprintf(redirected, sizeof(redirected), "%s%s", base, path + size);
144추가: if (n < 0 || (size_t)n >= sizeof(redirected)) {
145추가: errno = ENAMETOOLONG;
146추가: return -1;
147추가: }
148추가: return original(fd, redirected, flags, mask, info);
149추가: }
150추가: return original(fd, path, flags, mask, info);
151추가: }