Skip to contentSkip to conversation

quota: Verify special quota formats match sb type

PATCH In review Jan Kara 4 participants 7 messages Sep 3 – Sep 8

Subsystems (4)
lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

v2 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:

author reviewer
#1 lore raw

PATCH 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
index f55810c59b1b..a2b276bcd0a8 100644
@@ -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
maintainer Reviewed-by on v2 1/2
#2 lore raw

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_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.

Quoted 19 lines
---
 fs/ocfs2/quota_local.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/ocfs2/quota_local.c +4 -0
index f55810c59b1b..a2b276bcd0a8 100644
@@ -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) {
author reviewer
#3 lore raw

PATCH v2 2/2 shmem: Refuse to enable tmpfs format for another filesystem

In reply to Jan Kara

It 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
index d0b92d6da50f..a3fa4bd93e75 100644
@@ -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
maintainer Acked-by on v2 2/2
#4 lore raw

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_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.

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
index d0b92d6da50f..a3fa4bd93e75 100644
@@ -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
#5 lore raw

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_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.

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
index d0b92d6da50f..a3fa4bd93e75 100644
@@ -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 -1
 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

/*
* 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;
}
author reviewer
#6 lore raw

Re: [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_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.

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
index d0b92d6da50f..a3fa4bd93e75 100644
@@ -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