team-ljyzipelo

공개

상위 폴더의 탐색 권한만으로 ZIP 입력에 접근함

작성자
시로사키 하나
작성 시각
커밋
3eecc1da0989a3e8b08f8c19b1fe6fd66e62e551
부모
75563cc
이 시점의 파일 보기

변경된 파일 2개

+116-4
수정experiments/rust-archive-worker/src/create_sources.rs+16-4
@@ -27,7 +27,7 @@ impl Input {
2727 fn openat(fd: i32, name: *const i8, flags: i32, ...) -> i32;
2828 }
2929 let name = std::ffi::CString::new(name.as_bytes()).map_err(|_| ErrorCode::UnsafePath)?;
30삭제: // 객체 종류를 확인한 뒤 고정 핸들로만 읽기 열기를 수행함.
30추가: // 링크를 따라가지 않는 경로 핸들로 객체 종류를 확인함.
3131 let fd = unsafe {
3232 openat(
3333 file.as_raw_fd(),
@@ -45,6 +45,12 @@ impl Input {
4545 }
4646 let pinned = unsafe { File::from_raw_fd(fd) };
4747 Self::validate(&pinned)?;
48추가: Ok(pinned)
49추가: }
50추가: #[cfg(target_os = "linux")]
51추가: fn read(pinned: File) -> Result<File> {
52추가: use std::os::fd::AsRawFd;
53추가: // 읽기 권한은 최종 입력에만 요청함.
4854 File::open(format!("/proc/self/fd/{}", pinned.as_raw_fd())).map_err(crate::space::io_error)
4955 }
5056 pub fn open(path: &Path) -> Result<Self> {
@@ -53,7 +59,12 @@ impl Input {
5359 }
5460 #[cfg(target_os = "linux")]
5561 {
56삭제: let mut file = File::open("/").map_err(crate::space::io_error)?;
62추가: use std::os::unix::fs::OpenOptionsExt;
63추가: let mut file = fs::OpenOptions::new()
64추가: .read(true)
65추가: .custom_flags(0x200000 | 0x20000 | 0x10000 | 0x80000)
66추가: .open("/")
67추가: .map_err(crate::space::io_error)?;
5768 for part in path.components() {
5869 match part {
5970 Component::RootDir => {}
@@ -61,6 +72,7 @@ impl Input {
6172 _ => return Err(ErrorCode::UnsafePath),
6273 }
6374 }
75추가: let file = Self::read(file)?;
6476 Self::validate(&file)?;
6577 Ok(Self { file })
6678 }
@@ -99,7 +111,7 @@ impl Input {
99111 fn lock(path: &Path) -> Result<File> {
100112 use std::os::windows::fs::{MetadataExt, OpenOptionsExt};
101113 let file = fs::OpenOptions::new()
102삭제: .access_mode(0x80)
114추가: .access_mode(0)
103115 .share_mode(3)
104116 .custom_flags(0x02000000 | 0x00200000)
105117 .open(path)
@@ -133,7 +145,7 @@ impl Input {
133145 #[cfg(target_os = "linux")]
134146 {
135147 let _ = path;
136삭제: let file = Self::child(&self.file, std::ffi::OsStr::new(name))?;
148추가: let file = Self::read(Self::child(&self.file, std::ffi::OsStr::new(name))?)?;
137149 Self::validate(&file)?;
138150 Ok(Self { file })
139151 }
수정tests/rust-archive-worker/folder_inputs.rs+100-0
@@ -225,3 +225,103 @@ fn invalid_descendant_names_and_case_collisions_fail_before_password() {
225225 fs::remove_file(s.0.join("tree").join(leaf)).unwrap();
226226 }
227227}
228추가:
229추가: #[cfg(target_os = "linux")]
230추가: fn readable_sources_under_parent(mode: u32) {
231추가: use std::os::unix::fs::PermissionsExt;
232추가: struct RestoreParent(PathBuf);
233추가: impl Drop for RestoreParent {
234추가: fn drop(&mut self) {
235추가: fs::set_permissions(&self.0, fs::Permissions::from_mode(0o700)).unwrap();
236추가: }
237추가: }
238추가: let s = Scratch::new();
239추가: let parent = s.0.join("restricted");
240추가: fs::create_dir_all(parent.join("tree/empty")).unwrap();
241추가: fs::write(parent.join("loose"), b"readable file").unwrap();
242추가: fs::write(parent.join("tree/nested"), b"nested content").unwrap();
243추가: let _restore = RestoreParent(parent.clone());
244추가: fs::set_permissions(&parent, fs::Permissions::from_mode(mode)).unwrap();
245추가: if mode == 0o111 {
246추가: assert_eq!(
247추가: fs::read_dir(&parent).unwrap_err().kind(),
248추가: std::io::ErrorKind::PermissionDenied
249추가: );
250추가: } else {
251추가: assert!(fs::read_dir(&parent).is_ok());
252추가: }
253추가: assert_eq!(fs::read(parent.join("loose")).unwrap(), b"readable file");
254추가: for (name, encryption) in [("plain", Encryption::None), ("aes", Encryption::Aes256)] {
255추가: let archive_name = format!("{name}.zip");
256추가: let events = interactive(
257추가: Start {
258추가: action: Action::CreateZip {
259추가: sources: vec![
260추가: Source {
261추가: path: NativePath::from_path(&parent.join("loose")),
262추가: archive_name: "loose.txt".into(),
263추가: },
264추가: Source {
265추가: path: NativePath::from_path(&parent.join("tree")),
266추가: archive_name: "tree".into(),
267추가: },
268추가: ],
269추가: output_file: s.path(&archive_name),
270추가: method: Method::Deflate,
271추가: encryption,
272추가: },
273추가: limits: Limits::default(),
274추가: },
275추가: |id, _, _| {
276추가: vec![Message::Password {
277추가: challenge_id: id,
278추가: secret: Secret(b"parent-password".to_vec()),
279추가: }]
280추가: },
281추가: );
282추가: assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
283추가: let output = format!("output-{name}");
284추가: fs::create_dir(s.0.join(&output)).unwrap();
285추가: fs::set_permissions(s.0.join(&output), fs::Permissions::from_mode(0o700)).unwrap();
286추가: let extracted = interactive(
287추가: Start {
288추가: action: Action::Extract {
289추가: archive: s.path(&archive_name),
290추가: destination: s.path(&output),
291추가: selection: Selection::All,
292추가: list_id: None,
293추가: gzip_output_name: None,
294추가: },
295추가: limits: Limits::default(),
296추가: },
297추가: |id, _, _| {
298추가: vec![Message::Password {
299추가: challenge_id: id,
300추가: secret: Secret(b"parent-password".to_vec()),
301추가: }]
302추가: },
303추가: );
304추가: assert_eq!(
305추가: terminal(&extracted).outcome,
306추가: Outcome::Completed,
307추가: "{extracted:?}"
308추가: );
309추가: for (source, destination) in [("loose", "loose.txt"), ("tree/nested", "tree/nested")] {
310추가: assert_eq!(
311추가: Sha256::digest(fs::read(parent.join(source)).unwrap()),
312추가: Sha256::digest(fs::read(s.0.join(&output).join(destination)).unwrap())
313추가: );
314추가: }
315추가: assert!(s.0.join(&output).join("tree/empty").is_dir());
316추가: }
317추가: }
318추가: #[cfg(target_os = "linux")]
319추가: #[test]
320추가: fn readable_sources_under_search_only_parent_0111() {
321추가: readable_sources_under_parent(0o111);
322추가: }
323추가: #[cfg(target_os = "linux")]
324추가: #[test]
325추가: fn readable_sources_under_readable_parent_0711() {
326추가: readable_sources_under_parent(0o711);
327추가: }