개인 그룹의 기본 구성원 검증을 보완함
- 작성자
- 시로사키 하나
- 작성 시각
- 커밋
31eeb501ec69b264512babeb3eb3246079157ab8- 부모
- 26121d4
변경된 파일 4개
+281-40| @@ -210,11 +210,52 @@ mod platform { | ||
| 210 | 210 | gid: u32, |
| 211 | 211 | members: *mut *mut i8, |
| 212 | 212 | } |
| 213 | 삭제: /// 현재 계정의 기본 그룹이 따로 등록된 구성원이 없는 개인 그룹인지 확인함. | |
| 214 | 삭제: /// Ubuntu는 계정마다 같은 이름의 개인 그룹을 만들고 umask 002를 써서, | |
| 215 | 삭제: /// 파일 관리자나 폴더 선택 창에서 새로 만든 폴더가 그룹 쓰기 권한(775)을 가짐. | |
| 213 | 추가: // 원격 계정 열거는 불완전할 수 있으므로 로컬 계정만 확인 가능한 경우에 허용함. | |
| 214 | 추가: fn primary_group_is_private(gid: u32) -> bool { | |
| 215 | 추가: let Ok(policy) = std::fs::read_to_string("/etc/nsswitch.conf") else { | |
| 216 | 추가: return false; | |
| 217 | 추가: }; | |
| 218 | 추가: let mut sources = policy.lines().filter_map(|line| { | |
| 219 | 추가: let line = line.split('#').next().unwrap_or_default(); | |
| 220 | 추가: let (database, sources) = line.split_once(':')?; | |
| 221 | 추가: (database.trim() == "passwd").then_some(sources) | |
| 222 | 추가: }); | |
| 223 | 추가: let Some(source) = sources.next() else { | |
| 224 | 추가: return false; | |
| 225 | 추가: }; | |
| 226 | 추가: if sources.next().is_some() || !source.split_whitespace().eq(["files"]) { | |
| 227 | 추가: return false; | |
| 228 | 추가: } | |
| 229 | 추가: let Ok(accounts) = std::fs::read_to_string("/etc/passwd") else { | |
| 230 | 추가: return false; | |
| 231 | 추가: }; | |
| 232 | 추가: let current_uid = unsafe { geteuid() }; | |
| 233 | 추가: let mut found_current = false; | |
| 234 | 추가: for account in accounts.lines() { | |
| 235 | 추가: if account.trim().is_empty() || account.starts_with('#') { | |
| 236 | 추가: continue; | |
| 237 | 추가: } | |
| 238 | 추가: let fields: Vec<_> = account.split(':').collect(); | |
| 239 | 추가: if fields.len() != 7 || fields[0].is_empty() { | |
| 240 | 추가: return false; | |
| 241 | 추가: } | |
| 242 | 추가: let (Ok(uid), Ok(primary_gid)) = (fields[2].parse::<u32>(), fields[3].parse::<u32>()) | |
| 243 | 추가: else { | |
| 244 | 추가: return false; | |
| 245 | 추가: }; | |
| 246 | 추가: if primary_gid == gid { | |
| 247 | 추가: if uid != current_uid { | |
| 248 | 추가: return false; | |
| 249 | 추가: } | |
| 250 | 추가: found_current = true; | |
| 251 | 추가: } | |
| 252 | 추가: } | |
| 253 | 추가: found_current | |
| 254 | 추가: } | |
| 255 | 추가: | |
| 256 | 추가: // 기본 구성원과 보조 구성원을 모두 확인함. | |
| 216 | 257 | fn private_group(gid: u32) -> bool { |
| 217 | 삭제: if gid != unsafe { getegid() } { | |
| 258 | 추가: if gid != unsafe { getegid() } || !primary_group_is_private(gid) { | |
| 218 | 259 | return false; |
| 219 | 260 | } |
| 220 | 261 | let mut group = Group { |
| @@ -237,7 +278,9 @@ mod platform { | ||
| 237 | 278 | // 그룹 정보를 읽지 못하면 안전하다고 볼 근거가 없으므로 거부함. |
| 238 | 279 | status == 0 |
| 239 | 280 | && !result.is_null() |
| 240 | 삭제: && (group.members.is_null() || unsafe { *group.members }.is_null()) | |
| 281 | 추가: && group.gid == gid | |
| 282 | 추가: && !group.members.is_null() | |
| 283 | 추가: && unsafe { *group.members }.is_null() | |
| 241 | 284 | } |
| 242 | 285 | pub struct Dir { |
| 243 | 286 | file: File, |
| @@ -519,44 +519,13 @@ fn symlink_parent_escape_is_rejected() { | ||
| 519 | 519 | assert_eq!(fs::read_dir(s.0.join("outside")).unwrap().count(), 0); |
| 520 | 520 | } |
| 521 | 521 | |
| 522 | 삭제: // umask 002에서 폴더 선택 창으로 새로 만든 폴더(775)에도 풀 수 있어야 함. | |
| 523 | 삭제: // 다른 계정이 쓸 수 있는 폴더(777)는 계속 거부함. | |
| 522 | 추가: // 개인 그룹의 압축 해제만 허용하고 공유 쓰기는 거부함. | |
| 524 | 523 | #[cfg(target_os = "linux")] |
| 525 | 524 | #[test] |
| 526 | 525 | fn private_group_writable_output_is_accepted() { |
| 527 | 삭제: use std::os::unix::fs::{MetadataExt, PermissionsExt}; | |
| 528 | 삭제: for (mode, other) in [(0o775, false), (0o777, true)] { | |
| 529 | 삭제: let s = Scratch::new(); | |
| 530 | 삭제: fs::create_dir(s.0.join("output")).unwrap(); | |
| 531 | 삭제: fs::set_permissions(s.0.join("output"), fs::Permissions::from_mode(mode)).unwrap(); | |
| 532 | 삭제: let gid = fs::metadata(s.0.join("output")).unwrap().gid(); | |
| 533 | 삭제: let entry = Command::new("getent") | |
| 534 | 삭제: .args(["group", &gid.to_string()]) | |
| 535 | 삭제: .output() | |
| 536 | 삭제: .unwrap(); | |
| 537 | 삭제: let entry = String::from_utf8_lossy(&entry.stdout); | |
| 538 | 삭제: let egid = Command::new("id").arg("-g").output().unwrap(); | |
| 539 | 삭제: let private = String::from_utf8_lossy(&egid.stdout).trim() == gid.to_string() | |
| 540 | 삭제: && entry.trim_end().ends_with(':'); | |
| 541 | 삭제: let e = extract( | |
| 542 | 삭제: &s, | |
| 543 | 삭제: include_bytes!("fixtures/store.zip"), | |
| 544 | 삭제: None, | |
| 545 | 삭제: Selection::All, | |
| 546 | 삭제: None, | |
| 547 | 삭제: Limits::default(), | |
| 548 | 삭제: ); | |
| 549 | 삭제: if other || !private { | |
| 550 | 삭제: assert_eq!(terminal(&e).error, ErrorCode::UnsafePath, "{mode:o}"); | |
| 551 | 삭제: assert!(!s.0.join("output/folder").exists()); | |
| 552 | 삭제: } else { | |
| 553 | 삭제: assert_eq!(terminal(&e).outcome, Outcome::Completed, "{e:?}"); | |
| 554 | 삭제: assert_eq!( | |
| 555 | 삭제: fs::read(s.0.join("output/folder/hello.txt")).unwrap(), | |
| 556 | 삭제: PAYLOAD | |
| 557 | 삭제: ); | |
| 558 | 삭제: } | |
| 559 | 삭제: } | |
| 526 | 추가: private_group::check_extraction("personal", true); | |
| 527 | 추가: private_group::check_extraction("shared-primary", false); | |
| 528 | 추가: private_group::check_extraction("world-writable", false); | |
| 560 | 529 | } |
| 561 | 530 | |
| 562 | 531 | #[test] |
| @@ -1676,3 +1645,6 @@ mod spool_location; | ||
| 1676 | 1645 | |
| 1677 | 1646 | #[cfg(target_os = "linux")] |
| 1678 | 1647 | mod spool_fallback; |
| 1648 | 추가: | |
| 1649 | 추가: #[cfg(target_os = "linux")] | |
| 1650 | 추가: mod private_group; |
| @@ -0,0 +1,172 @@ | ||
| 1 | 추가: use super::*; | |
| 2 | 추가: use std::os::unix::fs::{MetadataExt, PermissionsExt}; | |
| 3 | 추가: | |
| 4 | 추가: fn check_case(case: &str, safe: bool) { | |
| 5 | 추가: run_case(case, safe, false); | |
| 6 | 추가: } | |
| 7 | 추가: | |
| 8 | 추가: pub(super) fn check_extraction(case: &str, safe: bool) { | |
| 9 | 추가: run_case(case, safe, true); | |
| 10 | 추가: } | |
| 11 | 추가: | |
| 12 | 추가: fn run_case(case: &str, safe: bool, extraction: bool) { | |
| 13 | 추가: let scratch = Scratch::new(); | |
| 14 | 추가: let output = scratch.0.join("output"); | |
| 15 | 추가: fs::create_dir(&output).unwrap(); | |
| 16 | 추가: let mode = if case == "world-writable" { | |
| 17 | 추가: 0o777 | |
| 18 | 추가: } else { | |
| 19 | 추가: 0o775 | |
| 20 | 추가: }; | |
| 21 | 추가: fs::set_permissions(&output, fs::Permissions::from_mode(mode)).unwrap(); | |
| 22 | 추가: if extraction { | |
| 23 | 추가: fs::write( | |
| 24 | 추가: scratch.0.join("input"), | |
| 25 | 추가: include_bytes!("fixtures/store.zip"), | |
| 26 | 추가: ) | |
| 27 | 추가: .unwrap(); | |
| 28 | 추가: } | |
| 29 | 추가: let metadata = fs::metadata(&output).unwrap(); | |
| 30 | 추가: let uid = metadata.uid(); | |
| 31 | 추가: let gid = metadata.gid(); | |
| 32 | 추가: let mut passwd = format!("owner:x:{uid}:{gid}::/:/bin/sh\n"); | |
| 33 | 추가: match case { | |
| 34 | 추가: "shared-primary" => passwd.push_str(&format!( | |
| 35 | 추가: "other:x:{}:{gid}::/:/bin/sh\n", | |
| 36 | 추가: uid.wrapping_add(1) | |
| 37 | 추가: )), | |
| 38 | 추가: "unrelated-primary" => passwd.push_str(&format!( | |
| 39 | 추가: "other:x:{}:{}::/:/bin/sh\n", | |
| 40 | 추가: uid.wrapping_add(1), | |
| 41 | 추가: gid.wrapping_add(1) | |
| 42 | 추가: )), | |
| 43 | 추가: "missing-owner" => passwd.clear(), | |
| 44 | 추가: "malformed-passwd" => passwd.push_str("broken:x:not-a-uid:1000::/:/bin/sh\n"), | |
| 45 | 추가: "nis-passwd" => passwd.push_str("+::::::\n"), | |
| 46 | 추가: _ => {} | |
| 47 | 추가: } | |
| 48 | 추가: let passwd_path = scratch.0.join("passwd"); | |
| 49 | 추가: fs::write(&passwd_path, passwd).unwrap(); | |
| 50 | 추가: if case == "passwd-unavailable" { | |
| 51 | 추가: fs::remove_file(&passwd_path).unwrap(); | |
| 52 | 추가: } | |
| 53 | 추가: let nss = match case { | |
| 54 | 추가: "remote-accounts" => "passwd: files sss\n", | |
| 55 | 추가: "unknown-accounts" => "passwd: files systemd\n", | |
| 56 | 추가: "duplicate-policy" => "passwd: files\npasswd: files\n", | |
| 57 | 추가: "missing-policy" => "group: files\n", | |
| 58 | 추가: _ => "# fixture\npasswd: files # local accounts\n", | |
| 59 | 추가: }; | |
| 60 | 추가: let nss_path = scratch.0.join("nsswitch.conf"); | |
| 61 | 추가: fs::write(&nss_path, nss).unwrap(); | |
| 62 | 추가: if case == "nss-unavailable" { | |
| 63 | 추가: fs::remove_file(&nss_path).unwrap(); | |
| 64 | 추가: } | |
| 65 | 추가: let library = scratch.0.join("private-group.so"); | |
| 66 | 추가: assert!( | |
| 67 | 추가: Command::new("cc") | |
| 68 | 추가: .args(["-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-Werror"]) | |
| 69 | 추가: .arg( | |
| 70 | 추가: PathBuf::from(env!("CARGO_MANIFEST_DIR")) | |
| 71 | 추가: .join("../../tests/rust-archive-worker/private_group_nss.c") | |
| 72 | 추가: ) | |
| 73 | 추가: .args(["-ldl", "-o"]) | |
| 74 | 추가: .arg(&library) | |
| 75 | 추가: .status() | |
| 76 | 추가: .unwrap() | |
| 77 | 추가: .success() | |
| 78 | 추가: ); | |
| 79 | 추가: let result = Command::new(std::env::current_exe().unwrap()) | |
| 80 | 추가: .args([ | |
| 81 | 추가: "--exact", | |
| 82 | 추가: "private_group::probe_child", | |
| 83 | 추가: "--test-threads=1", | |
| 84 | 추가: "--nocapture", | |
| 85 | 추가: ]) | |
| 86 | 추가: .env("LD_PRELOAD", library) | |
| 87 | 추가: .env("ZIPELO_GROUP_CASE", case) | |
| 88 | 추가: .env("ZIPELO_GROUP_PASSWD", passwd_path) | |
| 89 | 추가: .env("ZIPELO_GROUP_NSS", nss_path) | |
| 90 | 추가: .env("ZIPELO_GROUP_OUTPUT", &output) | |
| 91 | 추가: .env("ZIPELO_GROUP_SAFE", if safe { "1" } else { "0" }) | |
| 92 | 추가: .env("ZIPELO_GROUP_EXTRACT", if extraction { "1" } else { "0" }) | |
| 93 | 추가: .output() | |
| 94 | 추가: .unwrap(); | |
| 95 | 추가: assert!( | |
| 96 | 추가: result.status.success(), | |
| 97 | 추가: "{case}: {} {}", | |
| 98 | 추가: String::from_utf8_lossy(&result.stdout), | |
| 99 | 추가: String::from_utf8_lossy(&result.stderr) | |
| 100 | 추가: ); | |
| 101 | 추가: if extraction && safe { | |
| 102 | 추가: assert_eq!(fs::read(output.join("folder/hello.txt")).unwrap(), PAYLOAD); | |
| 103 | 추가: } else { | |
| 104 | 추가: assert_eq!(fs::read_dir(&output).unwrap().count(), 0); | |
| 105 | 추가: } | |
| 106 | 추가: assert_eq!(fs::metadata(&output).unwrap().mode() & 0o777, mode); | |
| 107 | 추가: } | |
| 108 | 추가: | |
| 109 | 추가: #[test] | |
| 110 | 추가: fn probe_child() { | |
| 111 | 추가: let Some(path) = std::env::var_os("ZIPELO_GROUP_OUTPUT") else { | |
| 112 | 추가: return; | |
| 113 | 추가: }; | |
| 114 | 추가: let path = std::path::Path::new(&path); | |
| 115 | 추가: let actual = zipelo_archive_worker::filesystem::Dir::root(path); | |
| 116 | 추가: if std::env::var("ZIPELO_GROUP_SAFE").unwrap() == "1" { | |
| 117 | 추가: assert!(actual.is_ok()); | |
| 118 | 추가: } else { | |
| 119 | 추가: assert!(matches!(actual, Err(ErrorCode::UnsafePath))); | |
| 120 | 추가: } | |
| 121 | 추가: if std::env::var("ZIPELO_GROUP_EXTRACT").unwrap() == "1" { | |
| 122 | 추가: let events = execute(Start { | |
| 123 | 추가: action: Action::Extract { | |
| 124 | 추가: archive: NativePath::from_path(&path.parent().unwrap().join("input")), | |
| 125 | 추가: destination: NativePath::from_path(path), | |
| 126 | 추가: selection: Selection::All, | |
| 127 | 추가: list_id: None, | |
| 128 | 추가: gzip_output_name: None, | |
| 129 | 추가: }, | |
| 130 | 추가: limits: Limits::default(), | |
| 131 | 추가: }); | |
| 132 | 추가: if std::env::var("ZIPELO_GROUP_SAFE").unwrap() == "1" { | |
| 133 | 추가: assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); | |
| 134 | 추가: } else { | |
| 135 | 추가: assert_eq!(terminal(&events).error, ErrorCode::UnsafePath, "{events:?}"); | |
| 136 | 추가: } | |
| 137 | 추가: } | |
| 138 | 추가: } | |
| 139 | 추가: | |
| 140 | 추가: #[test] | |
| 141 | 추가: fn private_group_accepts_only_current_primary_member() { | |
| 142 | 추가: for case in ["personal", "unrelated-primary"] { | |
| 143 | 추가: check_case(case, true); | |
| 144 | 추가: } | |
| 145 | 추가: } | |
| 146 | 추가: | |
| 147 | 추가: #[test] | |
| 148 | 추가: fn private_group_rejects_another_primary_member() { | |
| 149 | 추가: check_case("shared-primary", false); | |
| 150 | 추가: } | |
| 151 | 추가: | |
| 152 | 추가: #[test] | |
| 153 | 추가: fn private_group_rejects_incomplete_account_information() { | |
| 154 | 추가: for case in [ | |
| 155 | 추가: "missing-owner", | |
| 156 | 추가: "malformed-passwd", | |
| 157 | 추가: "nis-passwd", | |
| 158 | 추가: "passwd-unavailable", | |
| 159 | 추가: "nss-unavailable", | |
| 160 | 추가: "remote-accounts", | |
| 161 | 추가: "unknown-accounts", | |
| 162 | 추가: "duplicate-policy", | |
| 163 | 추가: "missing-policy", | |
| 164 | 추가: "group-error", | |
| 165 | 추가: "group-missing", | |
| 166 | 추가: "group-null-members", | |
| 167 | 추가: "group-wrong-gid", | |
| 168 | 추가: "supplementary-member", | |
| 169 | 추가: ] { | |
| 170 | 추가: check_case(case, false); | |
| 171 | 추가: } | |
| 172 | 추가: } |
| @@ -0,0 +1,54 @@ | ||
| 1 | 추가: #define _GNU_SOURCE | |
| 2 | 추가: #include <dlfcn.h> | |
| 3 | 추가: #include <errno.h> | |
| 4 | 추가: #include <fcntl.h> | |
| 5 | 추가: #include <grp.h> | |
| 6 | 추가: #include <stdarg.h> | |
| 7 | 추가: #include <stdlib.h> | |
| 8 | 추가: #include <string.h> | |
| 9 | 추가: | |
| 10 | 추가: int open64(const char *path, int flags, ...) { | |
| 11 | 추가: int (*original)(const char *, int, ...) = dlsym(RTLD_NEXT, "open64"); | |
| 12 | 추가: mode_t mode = 0; | |
| 13 | 추가: if (flags & O_CREAT) { | |
| 14 | 추가: va_list args; | |
| 15 | 추가: va_start(args, flags); | |
| 16 | 추가: mode = va_arg(args, unsigned int); | |
| 17 | 추가: va_end(args); | |
| 18 | 추가: } | |
| 19 | 추가: if (!strcmp(path, "/etc/passwd")) { | |
| 20 | 추가: path = getenv("ZIPELO_GROUP_PASSWD"); | |
| 21 | 추가: } else if (!strcmp(path, "/etc/nsswitch.conf")) { | |
| 22 | 추가: path = getenv("ZIPELO_GROUP_NSS"); | |
| 23 | 추가: } | |
| 24 | 추가: if (!path) { | |
| 25 | 추가: errno = EACCES; | |
| 26 | 추가: return -1; | |
| 27 | 추가: } | |
| 28 | 추가: return original(path, flags, mode); | |
| 29 | 추가: } | |
| 30 | 추가: | |
| 31 | 추가: int getgrgid_r(gid_t gid, struct group *group, char *buffer, size_t length, | |
| 32 | 추가: struct group **result) { | |
| 33 | 추가: (void)buffer; | |
| 34 | 추가: (void)length; | |
| 35 | 추가: const char *phase = getenv("ZIPELO_GROUP_CASE"); | |
| 36 | 추가: *result = NULL; | |
| 37 | 추가: if (!phase || !strcmp(phase, "group-error")) { | |
| 38 | 추가: return EIO; | |
| 39 | 추가: } | |
| 40 | 추가: if (!strcmp(phase, "group-missing")) { | |
| 41 | 추가: return 0; | |
| 42 | 추가: } | |
| 43 | 추가: static char *empty[] = {NULL}; | |
| 44 | 추가: static char *shared[] = {"other", NULL}; | |
| 45 | 추가: *group = (struct group){ | |
| 46 | 추가: .gr_name = "fixture", | |
| 47 | 추가: .gr_passwd = "x", | |
| 48 | 추가: .gr_gid = !strcmp(phase, "group-wrong-gid") ? gid + 1 : gid, | |
| 49 | 추가: .gr_mem = !strcmp(phase, "group-null-members") ? NULL : | |
| 50 | 추가: (!strcmp(phase, "supplementary-member") ? shared : empty) | |
| 51 | 추가: }; | |
| 52 | 추가: *result = group; | |
| 53 | 추가: return 0; | |
| 54 | 추가: } |