Re: [PATCH next V3] proc: support file->f_pos checking in mem_lseek
From: Oleg Nesterov
Date: Fri Nov 10 2023 - 12:58:46 EST
On 11/10, Zizhi Wo wrote:
>
> From: WoZ1zh1 <wozizhi@xxxxxxxxxx>
>
> In mem_lseek, file->f_pos may overflow. And it's not a problem that
> mem_open set file mode with FMODE_UNSIGNED_OFFSET(memory_lseek). However,
> another file use mem_lseek do lseek can have not FMODE_UNSIGNED_OFFSET
> (kpageflags_proc_ops/proc_pagemap_operations...), so in order to prevent
> file->f_pos updated to an abnormal number, fix it by checking overflow and
> FMODE_UNSIGNED_OFFSET.
I am wondering if we can do something like the patch below instead...
but I agree that the "proc_lseek == mem_lseek" in proc_reg_open()
looks ugly.
Oleg.
diff --git a/fs/proc/inode.c b/fs/proc/inode.c
index 532dc9d240f7..af7e6b1e17fe 100644
--- a/fs/proc/inode.c
+++ b/fs/proc/inode.c
@@ -496,6 +496,8 @@ static int proc_reg_open(struct inode *inode, struct file *file)
if (!pde->proc_ops->proc_lseek)
file->f_mode &= ~FMODE_LSEEK;
+ else if (pde->proc_ops->proc_lseek == mem_lseek)
+ file->f_mode |= FMODE_UNSIGNED_OFFSET;
if (pde_is_permanent(pde)) {
open = pde->proc_ops->proc_open;
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 3dd5be96691b..729b28ad1a96 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -1748,7 +1748,9 @@ static int pagemap_open(struct inode *inode, struct file *file)
mm = proc_mem_open(inode, PTRACE_MODE_READ);
if (IS_ERR(mm))
return PTR_ERR(mm);
+
file->private_data = mm;
+ file->f_mode |= FMODE_UNSIGNED_OFFSET;
return 0;
}