Skip to content

Commit 35c2a7f

Browse files
Hugh DickinsAl Viro
authored andcommitted
tmpfs,ceph,gfs2,isofs,reiserfs,xfs: fix fh_len checking
Fuzzing with trinity oopsed on the 1st instruction of shmem_fh_to_dentry(), u64 inum = fid->raw[2]; which is unhelpfully reported as at the end of shmem_alloc_inode(): BUG: unable to handle kernel paging request at ffff880061cd3000 IP: [<ffffffff812190d0>] shmem_alloc_inode+0x40/0x40 Oops: 0000 [raspberrypi#1] PREEMPT SMP DEBUG_PAGEALLOC Call Trace: [<ffffffff81488649>] ? exportfs_decode_fh+0x79/0x2d0 [<ffffffff812d77c3>] do_handle_open+0x163/0x2c0 [<ffffffff812d792c>] sys_open_by_handle_at+0xc/0x10 [<ffffffff83a5f3f8>] tracesys+0xe1/0xe6 Right, tmpfs is being stupid to access fid->raw[2] before validating that fh_len includes it: the buffer kmalloc'ed by do_sys_name_to_handle() may fall at the end of a page, and the next page not be present. But some other filesystems (ceph, gfs2, isofs, reiserfs, xfs) are being careless about fh_len too, in fh_to_dentry() and/or fh_to_parent(), and could oops in the same way: add the missing fh_len checks to those. Reported-by: Sasha Levin <levinsasha928@gmail.com> Signed-off-by: Hugh Dickins <hughd@google.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Sage Weil <sage@inktank.com> Cc: Steven Whitehouse <swhiteho@redhat.com> Cc: Christoph Hellwig <hch@infradead.org> Cc: stable@vger.kernel.org Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 8e22cc8 commit 35c2a7f

6 files changed

Lines changed: 31 additions & 8 deletions

File tree

‎fs/ceph/export.c‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,17 @@ static int ceph_encode_fh(struct inode *inode, u32 *rawfh, int *max_len,
9999
* FIXME: we should try harder by querying the mds for the ino.
100100
*/
101101
static struct dentry *__fh_to_dentry(struct super_block *sb,
102-
struct ceph_nfs_fh *fh)
102+
struct ceph_nfs_fh *fh, int fh_len)
103103
{
104104
struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
105105
struct inode *inode;
106106
struct dentry *dentry;
107107
struct ceph_vino vino;
108108
int err;
109109

110+
if (fh_len < sizeof(*fh) / 4)
111+
return ERR_PTR(-ESTALE);
112+
110113
dout("__fh_to_dentry %llx\n", fh->ino);
111114
vino.ino = fh->ino;
112115
vino.snap = CEPH_NOSNAP;
@@ -150,14 +153,17 @@ static struct dentry *__fh_to_dentry(struct super_block *sb,
150153
* convert connectable fh to dentry
151154
*/
152155
static struct dentry *__cfh_to_dentry(struct super_block *sb,
153-
struct ceph_nfs_confh *cfh)
156+
struct ceph_nfs_confh *cfh, int fh_len)
154157
{
155158
struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
156159
struct inode *inode;
157160
struct dentry *dentry;
158161
struct ceph_vino vino;
159162
int err;
160163

164+
if (fh_len < sizeof(*cfh) / 4)
165+
return ERR_PTR(-ESTALE);
166+
161167
dout("__cfh_to_dentry %llx (%llx/%x)\n",
162168
cfh->ino, cfh->parent_ino, cfh->parent_name_hash);
163169

@@ -207,9 +213,11 @@ static struct dentry *ceph_fh_to_dentry(struct super_block *sb, struct fid *fid,
207213
int fh_len, int fh_type)
208214
{
209215
if (fh_type == 1)
210-
return __fh_to_dentry(sb, (struct ceph_nfs_fh *)fid->raw);
216+
return __fh_to_dentry(sb, (struct ceph_nfs_fh *)fid->raw,
217+
fh_len);
211218
else
212-
return __cfh_to_dentry(sb, (struct ceph_nfs_confh *)fid->raw);
219+
return __cfh_to_dentry(sb, (struct ceph_nfs_confh *)fid->raw,
220+
fh_len);
213221
}
214222

215223
/*
@@ -230,6 +238,8 @@ static struct dentry *ceph_fh_to_parent(struct super_block *sb,
230238

231239
if (fh_type == 1)
232240
return ERR_PTR(-ESTALE);
241+
if (fh_len < sizeof(*cfh) / 4)
242+
return ERR_PTR(-ESTALE);
233243

234244
pr_debug("fh_to_parent %llx/%d\n", cfh->parent_ino,
235245
cfh->parent_name_hash);

‎fs/gfs2/export.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ static struct dentry *gfs2_fh_to_dentry(struct super_block *sb, struct fid *fid,
161161
case GFS2_SMALL_FH_SIZE:
162162
case GFS2_LARGE_FH_SIZE:
163163
case GFS2_OLD_FH_SIZE:
164+
if (fh_len < GFS2_SMALL_FH_SIZE)
165+
return NULL;
164166
this.no_formal_ino = ((u64)be32_to_cpu(fh[0])) << 32;
165167
this.no_formal_ino |= be32_to_cpu(fh[1]);
166168
this.no_addr = ((u64)be32_to_cpu(fh[2])) << 32;
@@ -180,6 +182,8 @@ static struct dentry *gfs2_fh_to_parent(struct super_block *sb, struct fid *fid,
180182
switch (fh_type) {
181183
case GFS2_LARGE_FH_SIZE:
182184
case GFS2_OLD_FH_SIZE:
185+
if (fh_len < GFS2_LARGE_FH_SIZE)
186+
return NULL;
183187
parent.no_formal_ino = ((u64)be32_to_cpu(fh[4])) << 32;
184188
parent.no_formal_ino |= be32_to_cpu(fh[5]);
185189
parent.no_addr = ((u64)be32_to_cpu(fh[6])) << 32;

‎fs/isofs/export.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ static struct dentry *isofs_fh_to_parent(struct super_block *sb,
175175
{
176176
struct isofs_fid *ifid = (struct isofs_fid *)fid;
177177

178-
if (fh_type != 2)
178+
if (fh_len < 2 || fh_type != 2)
179179
return NULL;
180180

181181
return isofs_export_iget(sb,

‎fs/reiserfs/inode.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1573,8 +1573,10 @@ struct dentry *reiserfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
15731573
reiserfs_warning(sb, "reiserfs-13077",
15741574
"nfsd/reiserfs, fhtype=%d, len=%d - odd",
15751575
fh_type, fh_len);
1576-
fh_type = 5;
1576+
fh_type = fh_len;
15771577
}
1578+
if (fh_len < 2)
1579+
return NULL;
15781580

15791581
return reiserfs_get_dentry(sb, fid->raw[0], fid->raw[1],
15801582
(fh_type == 3 || fh_type >= 5) ? fid->raw[2] : 0);
@@ -1583,6 +1585,8 @@ struct dentry *reiserfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
15831585
struct dentry *reiserfs_fh_to_parent(struct super_block *sb, struct fid *fid,
15841586
int fh_len, int fh_type)
15851587
{
1588+
if (fh_type > fh_len)
1589+
fh_type = fh_len;
15861590
if (fh_type < 4)
15871591
return NULL;
15881592

‎fs/xfs/xfs_export.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ xfs_fs_fh_to_parent(struct super_block *sb, struct fid *fid,
189189
struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fid;
190190
struct inode *inode = NULL;
191191

192+
if (fh_len < xfs_fileid_length(fileid_type))
193+
return NULL;
194+
192195
switch (fileid_type) {
193196
case FILEID_INO32_GEN_PARENT:
194197
inode = xfs_nfs_get_inode(sb, fid->i32.parent_ino,

‎mm/shmem.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,12 +2220,14 @@ static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
22202220
{
22212221
struct inode *inode;
22222222
struct dentry *dentry = NULL;
2223-
u64 inum = fid->raw[2];
2224-
inum = (inum << 32) | fid->raw[1];
2223+
u64 inum;
22252224

22262225
if (fh_len < 3)
22272226
return NULL;
22282227

2228+
inum = fid->raw[2];
2229+
inum = (inum << 32) | fid->raw[1];
2230+
22292231
inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
22302232
shmem_match, fid->raw);
22312233
if (inode) {

0 commit comments

Comments
 (0)