quota: Verify special quota formats match sb type
Subsystems (4)
Jan Kara
reviewerv2 cover quota: Verify special quota formats match sb type
Hello,
here is v2 of the patches for fix quota crashes when quota format doesn't match filesystem type. I'm now using s_magic instead of s_type for superblock type checking as Hugh pointed out (thus I also didn't pick up OCFS2 guys' R-b tags because the patch is different now). Once reviewed, I can merge these patches through my tree as they are only quota related and very unlikely to clash with anything.
Changes since v1:
* Use s_magic instead of s_type for superblock type checking
Original cover letter:
There are certain quota formats that are created only for particular filesystems - ocfs2 and tmpfs quota formats in particular. Make sure these formats are indeed being enabled only for filesystem of appropriate type. Otherwise quota format operations can easily crash as was spotted by fuzz testing.
Honza
Previous versions:
Jan Kara
reviewerPATCH v2 1/2 ocfs2: Refuse to enable ocfs2 format for another filesystem
It is possible to issue Q_QUOTAON quotactl(2) e.g. for ext4 filesystem requesting to enable quota using ocfs2 quota format. With suitable passed arguments this can get upto ocfs2_local_check_quota_file() where the passed superblock is from the ext4 filesystem and that can cause all sorts of interesting things, most likely crashes. As ocfs2 quotas are specific to ocfs2 filesystem, verify we are indeed enabling quotas there.
--- fs/ocfs2/quota_local.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/ocfs2/quota_local.c +4 -0
@@ -171,6 +171,10 @@ static int ocfs2_local_check_quota_file(struct super_block *sb, int type)struct ocfs2_disk_dqheader *dqhead;int status, ret =0 ; +/* OCFS2 quota format is supported only for OCFS2 filesystems */ +if (sb->s_magic !=OCFS2_SUPER_MAGIC ) + goto out_err; +/* First check whether we understand local quota file */ status =ocfs2_read_quota_block (linode,0 , &bh);if (status) {
Signature · 1 line
2.51.0
Re: [PATCH v2 1/2] ocfs2: Refuse to enable ocfs2 format for another filesystem
Quoted 9 lines · Jan Kara
It is possible to issue
Q_QUOTAONquotactl(2) e.g. for ext4 filesystem requesting to enable quota using ocfs2 quota format. With suitable passed arguments this can get uptoocfs2_local_check_quota_file()where the passed superblock is from the ext4 filesystem and that can cause all sorts of interesting things, most likely crashes. As ocfs2 quotas are specific to ocfs2 filesystem, verify we are indeed enabling quotas there.Signed-off-by Jan Kara <[email protected]>
Quoted 19 lines
--- fs/ocfs2/quota_local.c | 4 ++++ 1 file changed, 4 insertions(+)fs/ocfs2/quota_local.c +4
-0@@ -171,6 +171,10 @@ static int ocfs2_local_check_quota_file(struct super_block *sb, int type)struct ocfs2_disk_dqheader *dqhead;int status, ret =0 ; +/* OCFS2 quota format is supported only for OCFS2 filesystems */ +if (sb->s_magic !=OCFS2_SUPER_MAGIC ) + goto out_err; +/* First check whether we understand local quota file */ status =ocfs2_read_quota_block (linode,0 , &bh);if (status) {
Jan Kara
reviewerPATCH v2 2/2 shmem: Refuse to enable tmpfs format for another filesystem
In reply to Jan KaraIt is possible to issue Q_QUOTAON quotactl(2) e.g. for ext4 filesystem requesting to enable quota using tmpfs quota format. With suitable passed arguments this can actually succeed in enabling tmpfs quota format on top of ext4 filesystem which leads to crashes later in dquot_acquire(). As tmpfs quota format is intended for tmpfs filesystems only, verify we are indeed enabling quotas there.
--- mm/shmem_quota.c | 5 +++++ 1 file changed, 5 insertions(+)
mm/shmem_quota.c +5 -0
@@ -30,6 +30,7 @@#include <linux/slab.h> #include <linux/rbtree.h> #include <linux/shmem_fs.h> +#include <linux/magic.h> #include <linux/quotaops.h> #include <linux/quota.h>
@@ -54,6 +55,10 @@ struct quota_id {static int shmem_check_quota_file (struct super_block *sb,int type) { +/* Verify enabling happens on tmpfs superblock */ +if (sb->s_magic !=TMPFS_MAGIC ) +return 0 ; +/* There is no real quota file, nothing to do */ return 1 ; }
Signature · 1 line
2.51.0
Re: [PATCH v2 2/2] shmem: Refuse to enable tmpfs format for another filesystem
Quoted 9 lines · Jan Kara
It is possible to issue
Q_QUOTAONquotactl(2) e.g. for ext4 filesystem requesting to enable quota using tmpfs quota format. With suitable passed arguments this can actually succeed in enabling tmpfs quota format on top of ext4 filesystem which leads to crashes later indquot_acquire(). As tmpfs quota format is intended for tmpfs filesystems only, verify we are indeed enabling quotas there.Reported-by Farhad Alemi <[email protected]>Signed-off-by Jan Kara <[email protected]>
Thanks: yes, please do route it through your tree.
Quoted 29 lines
--- mm/shmem_quota.c | 5 +++++ 1 file changed, 5 insertions(+)mm/shmem_quota.c +5
-0@@ -30,6 +30,7 @@#include <linux/slab.h> #include <linux/rbtree.h> #include <linux/shmem_fs.h> +#include <linux/magic.h> #include <linux/quotaops.h> #include <linux/quota.h> @@ -54,6 +55,10 @@ struct quota_id {static int shmem_check_quota_file (struct super_block *sb,int type) { +/* Verify enabling happens on tmpfs superblock */ +if (sb->s_magic !=TMPFS_MAGIC ) +return 0 ; +/* There is no real quota file, nothing to do */ return 1 ; }Signature · 1 line
2.51.0
Re: [PATCH v2 2/2] shmem: Refuse to enable tmpfs format for another filesystem
Apologies for the delay in response. Upon our run, the patched kernel (this patch on 28924df2a08f) no longer panics on the attached reproducer.
Thanks!
Quoted 35 lines · Hugh Dickins
Quoted 9 lines · Jan Kara
It is possible to issue
Q_QUOTAONquotactl(2) e.g. for ext4 filesystem requesting to enable quota using tmpfs quota format. With suitable passed arguments this can actually succeed in enabling tmpfs quota format on top of ext4 filesystem which leads to crashes later indquot_acquire(). As tmpfs quota format is intended for tmpfs filesystems only, verify we are indeed enabling quotas there.Reported-by Farhad Alemi <[email protected]>Signed-off-by Jan Kara <[email protected]>Acked-by Hugh Dickins <[email protected]>Thanks: yes, please do route it through your tree.
Quoted 18 lines
--- mm/shmem_quota.c | 5 +++++ 1 file changed, 5 insertions(+)mm/shmem_quota.c +1
-0@@ -30,6 +30,7 @@#include <linux/slab.h> #include <linux/rbtree.h> #include <linux/shmem_fs.h> +#include <linux/magic.h> #include <linux/quotaops.h> #include <linux/quota.h> @@ -54,6 +55,10 @@ struct quota_id {
Quoted 11 lines
Quoted 11 lines
mm/shmem_quota.c +4
-1static int shmem_check_quota_file (struct super_block *sb,int type) { +/* Verify enabling happens on tmpfs superblock */ +if (sb->s_magic !=TMPFS_MAGIC ) +return 0 ; +/* There is no real quota file, nothing to do */ return 1 ; } --2.51.0
/*
* Reproducer for 103-quota-null-func-ptr-dquot_acquire */
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/quota.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/loop.h>
#define QFMT_SHMEM 5
int main(void)
{
char dev[32];
int fd, lfd, n;
fd = open("/tmp/i", O_RDWR | O_CREAT | O_TRUNC, 0600);
ftruncate(fd, 64 << 20);
close(fd);
system("mkfs.ext4 -qF -O ^quota /tmp/i");
fd = open("/dev/loop-control", O_RDWR);
n = ioctl(fd, LOOP_CTL_GET_FREE);
close(fd);
sprintf(dev, "/dev/loop%d", n);
lfd = open(dev, O_RDWR);
fd = open("/tmp/i", O_RDWR);
ioctl(lfd, LOOP_SET_FD, fd);
close(lfd); /* ext4 mounts the blockdev exclusively */
close(fd);
mkdir("/mnt/q", 0755);
mount(dev, "/mnt/q", "ext4", 0, "usrquota");
/* the quota file: shmem_check_quota_file() waves any file through */
close(open("/mnt/q/aquota.user", O_RDWR | O_CREAT, 0600));
/* a victim inode held open for write: add_dquot_ref() only visits
* inodes with a non-zero i_writecount */
open("/mnt/q/victim", O_RDWR | O_CREAT, 0600);
quotactl(QCMD(Q_QUOTAON, USRQUOTA), dev, QFMT_SHMEM,
(caddr_t)"/mnt/q/aquota.user");
return 0;
}Jan Kara
reviewerRe: [PATCH v2 2/2] shmem: Refuse to enable tmpfs format for another filesystem
Quoted 3 lines · Farhad Alemi
Apologies for the delay in response. Upon our run, the patched kernel (this patch on
28924df2a08f) no longer panics on the attached reproducer.
Thanks for testing!
Honza
Quoted 47 lines · Hugh Dickins
Quoted 46 lines · Jan Kara
Quoted 10 lines
It is possible to issue
Q_QUOTAONquotactl(2) e.g. for ext4 filesystem requesting to enable quota using tmpfs quota format. With suitable passed arguments this can actually succeed in enabling tmpfs quota format on top of ext4 filesystem which leads to crashes later indquot_acquire(). As tmpfs quota format is intended for tmpfs filesystems only, verify we are indeed enabling quotas there.Reported-by Farhad Alemi <[email protected]>Signed-off-by Jan Kara <[email protected]>Acked-by Hugh Dickins <[email protected]>Thanks: yes, please do route it through your tree.
Quoted 29 lines
--- mm/shmem_quota.c | 5 +++++ 1 file changed, 5 insertions(+)mm/shmem_quota.c +5
-1@@ -30,6 +30,7 @@#include <linux/slab.h> #include <linux/rbtree.h> #include <linux/shmem_fs.h> +#include <linux/magic.h> #include <linux/quotaops.h> #include <linux/quota.h> @@ -54,6 +55,10 @@ struct quota_id {static int shmem_check_quota_file (struct super_block *sb,int type) { +/* Verify enabling happens on tmpfs superblock */ +if (sb->s_magic !=TMPFS_MAGIC ) +return 0 ; +/* There is no real quota file, nothing to do */ return 1 ; } --2.51.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR