systemd 계정 열거로 개인 그룹 판정을 보완함
- 작성자
- 시로사키 하나
- 작성 시각
- 커밋
d439a949263f12fee30f1aa2d9d6bf4f64ad3062- 부모
- 31eeb50
변경된 파일 3개
+204-3| @@ -195,6 +195,9 @@ mod platform { | ||
| 195 | 195 | fn unlinkat(fd: i32, path: *const i8, flags: i32) -> i32; |
| 196 | 196 | fn geteuid() -> u32; |
| 197 | 197 | fn getegid() -> u32; |
| 198 | 추가: fn dlopen(name: *const i8, flags: i32) -> *mut std::ffi::c_void; | |
| 199 | 추가: fn dlsym(library: *mut std::ffi::c_void, name: *const i8) -> *mut std::ffi::c_void; | |
| 200 | 추가: fn dlclose(library: *mut std::ffi::c_void) -> i32; | |
| 198 | 201 | fn getgrgid_r( |
| 199 | 202 | gid: u32, |
| 200 | 203 | group: *mut Group, |
| @@ -210,7 +213,94 @@ mod platform { | ||
| 210 | 213 | gid: u32, |
| 211 | 214 | members: *mut *mut i8, |
| 212 | 215 | } |
| 213 | 삭제: // 원격 계정 열거는 불완전할 수 있으므로 로컬 계정만 확인 가능한 경우에 허용함. | |
| 216 | 추가: #[repr(C)] | |
| 217 | 추가: struct Passwd { | |
| 218 | 추가: name: *mut i8, | |
| 219 | 추가: password: *mut i8, | |
| 220 | 추가: uid: u32, | |
| 221 | 추가: gid: u32, | |
| 222 | 추가: gecos: *mut i8, | |
| 223 | 추가: directory: *mut i8, | |
| 224 | 추가: shell: *mut i8, | |
| 225 | 추가: } | |
| 226 | 추가: | |
| 227 | 추가: // glibc는 NSS 서비스 실패도 열거 종료로 바꾸므로 모듈의 상태를 직접 확인함. | |
| 228 | 추가: fn systemd_primary_group_is_private(gid: u32, uid: u32) -> bool { | |
| 229 | 추가: type Set = unsafe extern "C" fn(i32) -> i32; | |
| 230 | 추가: type End = unsafe extern "C" fn() -> i32; | |
| 231 | 추가: type Next = unsafe extern "C" fn(*mut Passwd, *mut i8, usize, *mut i32) -> i32; | |
| 232 | 추가: struct Library(*mut std::ffi::c_void); | |
| 233 | 추가: impl Drop for Library { | |
| 234 | 추가: fn drop(&mut self) { | |
| 235 | 추가: unsafe { dlclose(self.0) }; | |
| 236 | 추가: } | |
| 237 | 추가: } | |
| 238 | 추가: struct Enumeration(End); | |
| 239 | 추가: impl Drop for Enumeration { | |
| 240 | 추가: fn drop(&mut self) { | |
| 241 | 추가: unsafe { (self.0)() }; | |
| 242 | 추가: } | |
| 243 | 추가: } | |
| 244 | 추가: static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); | |
| 245 | 추가: let Ok(_lock) = LOCK.lock() else { | |
| 246 | 추가: return false; | |
| 247 | 추가: }; | |
| 248 | 추가: // RTLD_NOW로 모든 심볼을 확인하고 모듈이 없거나 열리지 않으면 거부함. | |
| 249 | 추가: let handle = unsafe { dlopen(c"libnss_systemd.so.2".as_ptr(), 2) }; | |
| 250 | 추가: if handle.is_null() { | |
| 251 | 추가: return false; | |
| 252 | 추가: } | |
| 253 | 추가: let library = Library(handle); | |
| 254 | 추가: let set = unsafe { dlsym(library.0, c"_nss_systemd_setpwent".as_ptr()) }; | |
| 255 | 추가: let end = unsafe { dlsym(library.0, c"_nss_systemd_endpwent".as_ptr()) }; | |
| 256 | 추가: let next = unsafe { dlsym(library.0, c"_nss_systemd_getpwent_r".as_ptr()) }; | |
| 257 | 추가: if set.is_null() || end.is_null() || next.is_null() { | |
| 258 | 추가: return false; | |
| 259 | 추가: } | |
| 260 | 추가: // NSS 모듈의 공개 C ABI와 일치하는 함수 포인터만 사용함. | |
| 261 | 추가: let (set, end, next) = unsafe { | |
| 262 | 추가: ( | |
| 263 | 추가: std::mem::transmute::<*mut std::ffi::c_void, Set>(set), | |
| 264 | 추가: std::mem::transmute::<*mut std::ffi::c_void, End>(end), | |
| 265 | 추가: std::mem::transmute::<*mut std::ffi::c_void, Next>(next), | |
| 266 | 추가: ) | |
| 267 | 추가: }; | |
| 268 | 추가: let _enumeration = Enumeration(end); | |
| 269 | 추가: if unsafe { set(0) } != 1 { | |
| 270 | 추가: return false; | |
| 271 | 추가: } | |
| 272 | 추가: let mut buffer = vec![0i8; 16 * 1024]; | |
| 273 | 추가: loop { | |
| 274 | 추가: let mut account = Passwd { | |
| 275 | 추가: name: std::ptr::null_mut(), | |
| 276 | 추가: password: std::ptr::null_mut(), | |
| 277 | 추가: uid: 0, | |
| 278 | 추가: gid: 0, | |
| 279 | 추가: gecos: std::ptr::null_mut(), | |
| 280 | 추가: directory: std::ptr::null_mut(), | |
| 281 | 추가: shell: std::ptr::null_mut(), | |
| 282 | 추가: }; | |
| 283 | 추가: let mut error = 0; | |
| 284 | 추가: let status = | |
| 285 | 추가: unsafe { next(&mut account, buffer.as_mut_ptr(), buffer.len(), &mut error) }; | |
| 286 | 추가: if status == -2 && error == 34 && buffer.len() < 1024 * 1024 { | |
| 287 | 추가: buffer.resize(buffer.len() * 2, 0); | |
| 288 | 추가: continue; | |
| 289 | 추가: } | |
| 290 | 추가: // NSS_STATUS_NOTFOUND만 정상 종료이며 UNAVAIL과 TRYAGAIN은 거부함. | |
| 291 | 추가: if status == 0 { | |
| 292 | 추가: return true; | |
| 293 | 추가: } | |
| 294 | 추가: if status != 1 || account.name.is_null() || unsafe { *account.name } == 0 { | |
| 295 | 추가: return false; | |
| 296 | 추가: } | |
| 297 | 추가: if account.gid == gid && account.uid != uid { | |
| 298 | 추가: return false; | |
| 299 | 추가: } | |
| 300 | 추가: } | |
| 301 | 추가: } | |
| 302 | 추가: | |
| 303 | 추가: // files와 systemd만 열거하며 원격 계정 소스와 임의의 NSS 제어 규칙은 거부함. | |
| 214 | 304 | fn primary_group_is_private(gid: u32) -> bool { |
| 215 | 305 | let Ok(policy) = std::fs::read_to_string("/etc/nsswitch.conf") else { |
| 216 | 306 | return false; |
| @@ -223,7 +313,10 @@ mod platform { | ||
| 223 | 313 | let Some(source) = sources.next() else { |
| 224 | 314 | return false; |
| 225 | 315 | }; |
| 226 | 삭제: if sources.next().is_some() || !source.split_whitespace().eq(["files"]) { | |
| 316 | 추가: if sources.next().is_some() | |
| 317 | 추가: || !(source.split_whitespace().eq(["files"]) | |
| 318 | 추가: || source.split_whitespace().eq(["files", "systemd"])) | |
| 319 | 추가: { | |
| 227 | 320 | return false; |
| 228 | 321 | } |
| 229 | 322 | let Ok(accounts) = std::fs::read_to_string("/etc/passwd") else { |
| @@ -251,6 +344,8 @@ mod platform { | ||
| 251 | 344 | } |
| 252 | 345 | } |
| 253 | 346 | found_current |
| 347 | 추가: && (!source.split_whitespace().any(|source| source == "systemd") | |
| 348 | 추가: || systemd_primary_group_is_private(gid, current_uid)) | |
| 254 | 349 | } |
| 255 | 350 | |
| 256 | 351 | // 기본 구성원과 보조 구성원을 모두 확인함. |
| @@ -52,7 +52,22 @@ fn run_case(case: &str, safe: bool, extraction: bool) { | ||
| 52 | 52 | } |
| 53 | 53 | let nss = match case { |
| 54 | 54 | "remote-accounts" => "passwd: files sss\n", |
| 55 | 삭제: "unknown-accounts" => "passwd: files systemd\n", | |
| 55 | 추가: "systemd-personal" | |
| 56 | 추가: | "systemd-dynamic-shared" | |
| 57 | 추가: | "systemd-homed-shared" | |
| 58 | 추가: | "systemd-unrelated" | |
| 59 | 추가: | "systemd-large-record" | |
| 60 | 추가: | "systemd-buffer-limit" | |
| 61 | 추가: | "systemd-enumeration-error" | |
| 62 | 추가: | "systemd-set-error" | |
| 63 | 추가: | "systemd-load-error" | |
| 64 | 추가: | "systemd-symbol-error" | |
| 65 | 추가: | "systemd-partial-error" => "passwd: files systemd\n", | |
| 66 | 추가: "ldap-accounts" => "passwd: files ldap\n", | |
| 67 | 추가: "winbind-accounts" => "passwd: files winbind\n", | |
| 68 | 추가: "systemd-remote" => "passwd: files systemd sss\n", | |
| 69 | 추가: "unknown-accounts" => "passwd: files unknown\n", | |
| 70 | 추가: "short-circuit-policy" => "passwd: files [SUCCESS=return] systemd\n", | |
| 56 | 71 | "duplicate-policy" => "passwd: files\npasswd: files\n", |
| 57 | 72 | "missing-policy" => "group: files\n", |
| 58 | 73 | _ => "# fixture\npasswd: files # local accounts\n", |
| @@ -159,6 +174,10 @@ fn private_group_rejects_incomplete_account_information() { | ||
| 159 | 174 | "nss-unavailable", |
| 160 | 175 | "remote-accounts", |
| 161 | 176 | "unknown-accounts", |
| 177 | 추가: "ldap-accounts", | |
| 178 | 추가: "winbind-accounts", | |
| 179 | 추가: "systemd-remote", | |
| 180 | 추가: "short-circuit-policy", | |
| 162 | 181 | "duplicate-policy", |
| 163 | 182 | "missing-policy", |
| 164 | 183 | "group-error", |
| @@ -170,3 +189,28 @@ fn private_group_rejects_incomplete_account_information() { | ||
| 170 | 189 | check_case(case, false); |
| 171 | 190 | } |
| 172 | 191 | } |
| 192 | 추가: | |
| 193 | 추가: #[test] | |
| 194 | 추가: fn private_group_checks_systemd_enumerated_accounts() { | |
| 195 | 추가: for case in [ | |
| 196 | 추가: "systemd-personal", | |
| 197 | 추가: "systemd-unrelated", | |
| 198 | 추가: "systemd-large-record", | |
| 199 | 추가: ] { | |
| 200 | 추가: check_extraction(case, true); | |
| 201 | 추가: } | |
| 202 | 추가: for case in [ | |
| 203 | 추가: "systemd-dynamic-shared", | |
| 204 | 추가: "systemd-homed-shared", | |
| 205 | 추가: "systemd-buffer-limit", | |
| 206 | 추가: "systemd-enumeration-error", | |
| 207 | 추가: "systemd-set-error", | |
| 208 | 추가: "systemd-load-error", | |
| 209 | 추가: "systemd-symbol-error", | |
| 210 | 추가: "systemd-partial-error", | |
| 211 | 추가: ] { | |
| 212 | 추가: check_extraction(case, false); | |
| 213 | 추가: } | |
| 214 | 추가: check_extraction("remote-accounts", false); | |
| 215 | 추가: check_extraction("systemd-remote", false); | |
| 216 | 추가: } |
| @@ -3,6 +3,8 @@ | ||
| 3 | 3 | #include <errno.h> |
| 4 | 4 | #include <fcntl.h> |
| 5 | 5 | #include <grp.h> |
| 6 | 추가: #include <pwd.h> | |
| 7 | 추가: #include <unistd.h> | |
| 6 | 8 | #include <stdarg.h> |
| 7 | 9 | #include <stdlib.h> |
| 8 | 10 | #include <string.h> |
| @@ -52,3 +54,63 @@ int getgrgid_r(gid_t gid, struct group *group, char *buffer, size_t length, | ||
| 52 | 54 | *result = group; |
| 53 | 55 | return 0; |
| 54 | 56 | } |
| 57 | 추가: | |
| 58 | 추가: void *dlopen(const char *name, int flags) { | |
| 59 | 추가: void *(*original)(const char *, int) = dlsym(RTLD_NEXT, "dlopen"); | |
| 60 | 추가: const char *phase = getenv("ZIPELO_GROUP_CASE"); | |
| 61 | 추가: if (name && !strcmp(name, "libnss_systemd.so.2")) { | |
| 62 | 추가: if (phase && !strcmp(phase, "systemd-load-error")) { | |
| 63 | 추가: return NULL; | |
| 64 | 추가: } | |
| 65 | 추가: if (phase && !strcmp(phase, "systemd-symbol-error")) { | |
| 66 | 추가: return original("libm.so.6", flags); | |
| 67 | 추가: } | |
| 68 | 추가: name = getenv("LD_PRELOAD"); | |
| 69 | 추가: } | |
| 70 | 추가: return original(name, flags); | |
| 71 | 추가: } | |
| 72 | 추가: | |
| 73 | 추가: static int systemd_record; | |
| 74 | 추가: | |
| 75 | 추가: int _nss_systemd_setpwent(int stayopen) { | |
| 76 | 추가: (void)stayopen; | |
| 77 | 추가: systemd_record = 0; | |
| 78 | 추가: const char *phase = getenv("ZIPELO_GROUP_CASE"); | |
| 79 | 추가: return phase && !strcmp(phase, "systemd-set-error") ? -1 : 1; | |
| 80 | 추가: } | |
| 81 | 추가: | |
| 82 | 추가: int _nss_systemd_endpwent(void) { | |
| 83 | 추가: return 1; | |
| 84 | 추가: } | |
| 85 | 추가: | |
| 86 | 추가: int _nss_systemd_getpwent_r(struct passwd *account, char *buffer, size_t length, | |
| 87 | 추가: int *error) { | |
| 88 | 추가: (void)buffer; | |
| 89 | 추가: const char *phase = getenv("ZIPELO_GROUP_CASE"); | |
| 90 | 추가: if (!phase || !strcmp(phase, "systemd-enumeration-error") || | |
| 91 | 추가: (systemd_record && !strcmp(phase, "systemd-partial-error"))) { | |
| 92 | 추가: *error = EIO; | |
| 93 | 추가: return -1; | |
| 94 | 추가: } | |
| 95 | 추가: if ((!strcmp(phase, "systemd-large-record") && length < 32768) || | |
| 96 | 추가: !strcmp(phase, "systemd-buffer-limit")) { | |
| 97 | 추가: *error = ERANGE; | |
| 98 | 추가: return -2; | |
| 99 | 추가: } | |
| 100 | 추가: if (systemd_record || !strcmp(phase, "systemd-personal")) { | |
| 101 | 추가: return 0; | |
| 102 | 추가: } | |
| 103 | 추가: systemd_record = 1; | |
| 104 | 추가: *account = (struct passwd){ | |
| 105 | 추가: .pw_name = "systemd-fixture", | |
| 106 | 추가: .pw_passwd = "x", | |
| 107 | 추가: .pw_uid = geteuid() + 1, | |
| 108 | 추가: .pw_gid = getegid() + | |
| 109 | 추가: ((strcmp(phase, "systemd-dynamic-shared") && | |
| 110 | 추가: strcmp(phase, "systemd-homed-shared")) ? 1 : 0), | |
| 111 | 추가: .pw_gecos = "", | |
| 112 | 추가: .pw_dir = "/", | |
| 113 | 추가: .pw_shell = "/bin/false" | |
| 114 | 추가: }; | |
| 115 | 추가: return 1; | |
| 116 | 추가: } |