Skip to content

Commit

Permalink
ANDROID: Add filp_open_block() for zram
Browse files Browse the repository at this point in the history
We currently plan to disallow use of filp_open() from drivers in GKI,
however the ZRAM driver still needs it. Add a new GKI-only variant of
filp_open() which only permits a block device to be opened, which can
be exported instead. This keeps ZRAM working but cuts down on drivers
that attempt to open and write files in kernel mode.

Bug: 179220339
Bug: 205141088
Change-Id: Id696b4aaf204b0499ce0a1b6416648670236e570
Signed-off-by: Alistair Delva <[email protected]>
(cherry picked from commit 62d9cb66f6bf79c6dbe7c556184ccd8cc791d719)
  • Loading branch information
adelva1984 authored and Treehugger Robot committed Apr 21, 2023
1 parent 3183446 commit d7595b4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion drivers/block/zram/zram_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ static ssize_t backing_dev_store(struct device *dev,
if (sz > 0 && file_name[sz - 1] == '\n')
file_name[sz - 1] = 0x00;

backing_dev = filp_open(file_name, O_RDWR|O_LARGEFILE, 0);
backing_dev = filp_open_block(file_name, O_RDWR|O_LARGEFILE, 0);
if (IS_ERR(backing_dev)) {
err = PTR_ERR(backing_dev);
backing_dev = NULL;
Expand Down
21 changes: 21 additions & 0 deletions fs/open.c
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,27 @@ struct file *filp_open(const char *filename, int flags, umode_t mode)
}
EXPORT_SYMBOL(filp_open);


/* ANDROID: Allow drivers to open only block files from kernel mode */
struct file *filp_open_block(const char *filename, int flags, umode_t mode)
{
struct file *file;

file = filp_open(filename, flags, mode);
if (IS_ERR(file))
goto err_out;

/* Drivers should only be allowed to open block devices */
if (!S_ISBLK(file->f_mapping->host->i_mode)) {
filp_close(file, NULL);
file = ERR_PTR(-ENOTBLK);
}

err_out:
return file;
}
EXPORT_SYMBOL_GPL(filp_open_block);

struct file *file_open_root(const struct path *root,
const char *filename, int flags, umode_t mode)
{
Expand Down
1 change: 1 addition & 0 deletions include/linux/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2771,6 +2771,7 @@ extern long do_sys_open(int dfd, const char __user *filename, int flags,
umode_t mode);
extern struct file *file_open_name(struct filename *, int, umode_t);
extern struct file *filp_open(const char *, int, umode_t);
extern struct file *filp_open_block(const char *, int, umode_t);
extern struct file *file_open_root(const struct path *,
const char *, int, umode_t);
static inline struct file *file_open_root_mnt(struct vfsmount *mnt,
Expand Down

0 comments on commit d7595b4

Please sign in to comment.