Add new_fd range check for dup2/dup3

Signed-off-by: Zheng, Qi <huaiqing.zq@antgroup.com>
This commit is contained in:
Zheng, Qi 2021-10-21 14:16:47 +08:00 committed by Zongmin.Gu
parent 3c9e172550
commit 1eb58a5eb3

@ -1,4 +1,5 @@
use super::*;
use misc::resource_t;
pub fn do_dup(old_fd: FileDesc) -> Result<FileDesc> {
let current = current!();
@ -11,6 +12,16 @@ pub fn do_dup2(old_fd: FileDesc, new_fd: FileDesc) -> Result<FileDesc> {
let current = current!();
let mut files = current.files().lock().unwrap();
let file = files.get(old_fd)?;
let soft_rlimit_nofile = current!()
.rlimits()
.lock()
.unwrap()
.get(resource_t::RLIMIT_NOFILE)
.get_cur();
if new_fd as u64 >= soft_rlimit_nofile {
return_errno!(EBADF, "Invalid new_fd file descriptor");
}
if old_fd != new_fd {
files.put_at(new_fd, file, false);
}
@ -22,6 +33,16 @@ pub fn do_dup3(old_fd: FileDesc, new_fd: FileDesc, flags: u32) -> Result<FileDes
let current = current!();
let mut files = current.files().lock().unwrap();
let file = files.get(old_fd)?;
let soft_rlimit_nofile = current!()
.rlimits()
.lock()
.unwrap()
.get(resource_t::RLIMIT_NOFILE)
.get_cur();
if new_fd as u64 >= soft_rlimit_nofile {
return_errno!(EBADF, "Invalid new_fd file descriptor");
}
if old_fd == new_fd {
return_errno!(EINVAL, "old_fd must not be equal to new_fd");
}