[Devel] [PATCH VZ9] dm-ploop validate file descriptors more strictly

Alexander Atanasov alexander.atanasov at virtuozzo.com
Tue Sep 20 11:26:07 MSK 2022


Check if file is opened with O_DIRECT.
Check if the file is a regular file.
If missing read permission return EACCES instead of EBADF.

https://jira.sw.ru/browse/PSBM-142046
Signed-off-by: Alexander Atanasov <alexander.atanasov at virtuozzo.com>
---
 drivers/md/dm-ploop-target.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
index bc4a801ca3a8..0e074b29e823 100644
--- a/drivers/md/dm-ploop-target.c
+++ b/drivers/md/dm-ploop-target.c
@@ -188,16 +188,32 @@ static void ploop_destroy(struct ploop *ploop)
 static struct file *ploop_get_delta_file(int fd)
 {
 	struct file *file;
+	int ret;
 
 	file = fget(fd);
 	if (!file)
 		return ERR_PTR(-ENOENT);
+
+	if (!S_ISREG(file_inode(file)->i_mode)) {
+		ret = -EINVAL;
+		goto out_err;
+	}
+
+	if (!(file->f_flags & O_DIRECT)) {
+		ret = -EINVAL;
+		goto out_err;
+	}
+
 	if (!(file->f_mode & FMODE_READ)) {
-		fput(file);
-		return ERR_PTR(-EBADF);
+		ret = -EACCES;
+		goto out_err;
 	}
 
 	return file;
+out_err:
+	fput(file);
+	return ERR_PTR(ret);
+
 }
 ALLOW_ERROR_INJECTION(ploop_get_delta_file, ERRNO_NULL);
 
-- 
2.31.1



More information about the Devel mailing list