Skip to content

Commit 36cf9ae

Browse files
Miklos Szeredigregkh
authored andcommitted
fuse: fix bad inode
[ Upstream commit 5d069db ] Jan Kara's analysis of the syzbot report (edited): The reproducer opens a directory on FUSE filesystem, it then attaches dnotify mark to the open directory. After that a fuse_do_getattr() call finds that attributes returned by the server are inconsistent, and calls make_bad_inode() which, among other things does: inode->i_mode = S_IFREG; This then confuses dnotify which doesn't tear down its structures properly and eventually crashes. Avoid calling make_bad_inode() on a live inode: switch to a private flag on the fuse inode. Also add the test to ops which the bad_inode_ops would have caught. This bug goes back to the initial merge of fuse in 2.6.14... Reported-by: syzbot+f427adf9324b92652ccc@syzkaller.appspotmail.com Signed-off-by: Miklos Szeredi <mszeredi@redhat.com> Tested-by: Jan Kara <jack@suse.cz> Cc: <stable@vger.kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent e522a78 commit 36cf9ae

7 files changed

Lines changed: 74 additions & 17 deletions

File tree

‎fs/fuse/acl.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ struct posix_acl *fuse_get_acl(struct inode *inode, int type)
1919
void *value = NULL;
2020
struct posix_acl *acl;
2121

22+
if (fuse_is_bad(inode))
23+
return ERR_PTR(-EIO);
24+
2225
if (!fc->posix_acl || fc->no_getxattr)
2326
return NULL;
2427

@@ -53,6 +56,9 @@ int fuse_set_acl(struct inode *inode, struct posix_acl *acl, int type)
5356
const char *name;
5457
int ret;
5558

59+
if (fuse_is_bad(inode))
60+
return -EIO;
61+
5662
if (!fc->posix_acl || fc->no_setxattr)
5763
return -EOPNOTSUPP;
5864

‎fs/fuse/dir.c‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
202202
int ret;
203203

204204
inode = d_inode_rcu(entry);
205-
if (inode && is_bad_inode(inode))
205+
if (inode && fuse_is_bad(inode))
206206
goto invalid;
207207
else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
208208
(flags & LOOKUP_REVAL)) {
@@ -463,6 +463,9 @@ static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
463463
bool outarg_valid = true;
464464
bool locked;
465465

466+
if (fuse_is_bad(dir))
467+
return ERR_PTR(-EIO);
468+
466469
locked = fuse_lock_inode(dir);
467470
err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
468471
&outarg, &inode);
@@ -606,6 +609,9 @@ static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
606609
struct fuse_conn *fc = get_fuse_conn(dir);
607610
struct dentry *res = NULL;
608611

612+
if (fuse_is_bad(dir))
613+
return -EIO;
614+
609615
if (d_in_lookup(entry)) {
610616
res = fuse_lookup(dir, entry, 0);
611617
if (IS_ERR(res))
@@ -654,6 +660,9 @@ static int create_new_entry(struct fuse_mount *fm, struct fuse_args *args,
654660
int err;
655661
struct fuse_forget_link *forget;
656662

663+
if (fuse_is_bad(dir))
664+
return -EIO;
665+
657666
forget = fuse_alloc_forget();
658667
if (!forget)
659668
return -ENOMEM;
@@ -781,6 +790,9 @@ static int fuse_unlink(struct inode *dir, struct dentry *entry)
781790
struct fuse_mount *fm = get_fuse_mount(dir);
782791
FUSE_ARGS(args);
783792

793+
if (fuse_is_bad(dir))
794+
return -EIO;
795+
784796
args.opcode = FUSE_UNLINK;
785797
args.nodeid = get_node_id(dir);
786798
args.in_numargs = 1;
@@ -817,6 +829,9 @@ static int fuse_rmdir(struct inode *dir, struct dentry *entry)
817829
struct fuse_mount *fm = get_fuse_mount(dir);
818830
FUSE_ARGS(args);
819831

832+
if (fuse_is_bad(dir))
833+
return -EIO;
834+
820835
args.opcode = FUSE_RMDIR;
821836
args.nodeid = get_node_id(dir);
822837
args.in_numargs = 1;
@@ -895,6 +910,9 @@ static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
895910
struct fuse_conn *fc = get_fuse_conn(olddir);
896911
int err;
897912

913+
if (fuse_is_bad(olddir))
914+
return -EIO;
915+
898916
if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
899917
return -EINVAL;
900918

@@ -1030,7 +1048,7 @@ static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
10301048
if (!err) {
10311049
if (fuse_invalid_attr(&outarg.attr) ||
10321050
(inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1033-
make_bad_inode(inode);
1051+
fuse_make_bad(inode);
10341052
err = -EIO;
10351053
} else {
10361054
fuse_change_attributes(inode, &outarg.attr,
@@ -1232,6 +1250,9 @@ static int fuse_permission(struct inode *inode, int mask)
12321250
bool refreshed = false;
12331251
int err = 0;
12341252

1253+
if (fuse_is_bad(inode))
1254+
return -EIO;
1255+
12351256
if (!fuse_allow_current_process(fc))
12361257
return -EACCES;
12371258

@@ -1327,7 +1348,7 @@ static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
13271348
int err;
13281349

13291350
err = -EIO;
1330-
if (is_bad_inode(inode))
1351+
if (fuse_is_bad(inode))
13311352
goto out_err;
13321353

13331354
if (fc->cache_symlinks)
@@ -1375,7 +1396,7 @@ static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
13751396
struct fuse_conn *fc = get_fuse_conn(inode);
13761397
int err;
13771398

1378-
if (is_bad_inode(inode))
1399+
if (fuse_is_bad(inode))
13791400
return -EIO;
13801401

13811402
if (fc->no_fsyncdir)
@@ -1664,7 +1685,7 @@ int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
16641685

16651686
if (fuse_invalid_attr(&outarg.attr) ||
16661687
(inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1667-
make_bad_inode(inode);
1688+
fuse_make_bad(inode);
16681689
err = -EIO;
16691690
goto error;
16701691
}
@@ -1727,6 +1748,9 @@ static int fuse_setattr(struct dentry *entry, struct iattr *attr)
17271748
struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
17281749
int ret;
17291750

1751+
if (fuse_is_bad(inode))
1752+
return -EIO;
1753+
17301754
if (!fuse_allow_current_process(get_fuse_conn(inode)))
17311755
return -EACCES;
17321756

@@ -1785,6 +1809,9 @@ static int fuse_getattr(const struct path *path, struct kstat *stat,
17851809
struct inode *inode = d_inode(path->dentry);
17861810
struct fuse_conn *fc = get_fuse_conn(inode);
17871811

1812+
if (fuse_is_bad(inode))
1813+
return -EIO;
1814+
17881815
if (!fuse_allow_current_process(fc)) {
17891816
if (!request_mask) {
17901817
/*

‎fs/fuse/file.c‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
226226
bool dax_truncate = (file->f_flags & O_TRUNC) &&
227227
fc->atomic_o_trunc && FUSE_IS_DAX(inode);
228228

229+
if (fuse_is_bad(inode))
230+
return -EIO;
231+
229232
err = generic_file_open(inode, file);
230233
if (err)
231234
return err;
@@ -463,7 +466,7 @@ static int fuse_flush(struct file *file, fl_owner_t id)
463466
FUSE_ARGS(args);
464467
int err;
465468

466-
if (is_bad_inode(inode))
469+
if (fuse_is_bad(inode))
467470
return -EIO;
468471

469472
err = write_inode_now(inode, 1);
@@ -535,7 +538,7 @@ static int fuse_fsync(struct file *file, loff_t start, loff_t end,
535538
struct fuse_conn *fc = get_fuse_conn(inode);
536539
int err;
537540

538-
if (is_bad_inode(inode))
541+
if (fuse_is_bad(inode))
539542
return -EIO;
540543

541544
inode_lock(inode);
@@ -859,7 +862,7 @@ static int fuse_readpage(struct file *file, struct page *page)
859862
int err;
860863

861864
err = -EIO;
862-
if (is_bad_inode(inode))
865+
if (fuse_is_bad(inode))
863866
goto out;
864867

865868
err = fuse_do_readpage(file, page);
@@ -952,7 +955,7 @@ static void fuse_readahead(struct readahead_control *rac)
952955
struct fuse_conn *fc = get_fuse_conn(inode);
953956
unsigned int i, max_pages, nr_pages = 0;
954957

955-
if (is_bad_inode(inode))
958+
if (fuse_is_bad(inode))
956959
return;
957960

958961
max_pages = min_t(unsigned int, fc->max_pages,
@@ -1555,7 +1558,7 @@ static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
15551558
struct fuse_file *ff = file->private_data;
15561559
struct inode *inode = file_inode(file);
15571560

1558-
if (is_bad_inode(inode))
1561+
if (fuse_is_bad(inode))
15591562
return -EIO;
15601563

15611564
if (FUSE_IS_DAX(inode))
@@ -1573,7 +1576,7 @@ static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
15731576
struct fuse_file *ff = file->private_data;
15741577
struct inode *inode = file_inode(file);
15751578

1576-
if (is_bad_inode(inode))
1579+
if (fuse_is_bad(inode))
15771580
return -EIO;
15781581

15791582
if (FUSE_IS_DAX(inode))
@@ -2172,7 +2175,7 @@ static int fuse_writepages(struct address_space *mapping,
21722175
int err;
21732176

21742177
err = -EIO;
2175-
if (is_bad_inode(inode))
2178+
if (fuse_is_bad(inode))
21762179
goto out;
21772180

21782181
data.inode = inode;
@@ -2954,7 +2957,7 @@ long fuse_ioctl_common(struct file *file, unsigned int cmd,
29542957
if (!fuse_allow_current_process(fc))
29552958
return -EACCES;
29562959

2957-
if (is_bad_inode(inode))
2960+
if (fuse_is_bad(inode))
29582961
return -EIO;
29592962

29602963
return fuse_do_ioctl(file, cmd, arg, flags);

‎fs/fuse/fuse_i.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ enum {
172172
FUSE_I_INIT_RDPLUS,
173173
/** An operation changing file size is in progress */
174174
FUSE_I_SIZE_UNSTABLE,
175+
/* Bad inode */
176+
FUSE_I_BAD,
175177
};
176178

177179
struct fuse_conn;
@@ -858,6 +860,16 @@ static inline u64 fuse_get_attr_version(struct fuse_conn *fc)
858860
return atomic64_read(&fc->attr_version);
859861
}
860862

863+
static inline void fuse_make_bad(struct inode *inode)
864+
{
865+
set_bit(FUSE_I_BAD, &get_fuse_inode(inode)->state);
866+
}
867+
868+
static inline bool fuse_is_bad(struct inode *inode)
869+
{
870+
return unlikely(test_bit(FUSE_I_BAD, &get_fuse_inode(inode)->state));
871+
}
872+
861873
/** Device operations */
862874
extern const struct file_operations fuse_dev_operations;
863875

‎fs/fuse/inode.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ static void fuse_evict_inode(struct inode *inode)
132132
fi->forget = NULL;
133133
}
134134
}
135-
if (S_ISREG(inode->i_mode) && !is_bad_inode(inode)) {
135+
if (S_ISREG(inode->i_mode) && !fuse_is_bad(inode)) {
136136
WARN_ON(!list_empty(&fi->write_files));
137137
WARN_ON(!list_empty(&fi->queued_writes));
138138
}
@@ -342,7 +342,7 @@ struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
342342
unlock_new_inode(inode);
343343
} else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
344344
/* Inode has changed type, any I/O on the old should fail */
345-
make_bad_inode(inode);
345+
fuse_make_bad(inode);
346346
iput(inode);
347347
goto retry;
348348
}

‎fs/fuse/readdir.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ static int fuse_direntplus_link(struct file *file,
207207
dput(dentry);
208208
goto retry;
209209
}
210-
if (is_bad_inode(inode)) {
210+
if (fuse_is_bad(inode)) {
211211
dput(dentry);
212212
return -EIO;
213213
}
@@ -568,7 +568,7 @@ int fuse_readdir(struct file *file, struct dir_context *ctx)
568568
struct inode *inode = file_inode(file);
569569
int err;
570570

571-
if (is_bad_inode(inode))
571+
if (fuse_is_bad(inode))
572572
return -EIO;
573573

574574
mutex_lock(&ff->readdir.lock);

‎fs/fuse/xattr.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
113113
struct fuse_getxattr_out outarg;
114114
ssize_t ret;
115115

116+
if (fuse_is_bad(inode))
117+
return -EIO;
118+
116119
if (!fuse_allow_current_process(fm->fc))
117120
return -EACCES;
118121

@@ -178,6 +181,9 @@ static int fuse_xattr_get(const struct xattr_handler *handler,
178181
struct dentry *dentry, struct inode *inode,
179182
const char *name, void *value, size_t size)
180183
{
184+
if (fuse_is_bad(inode))
185+
return -EIO;
186+
181187
return fuse_getxattr(inode, name, value, size);
182188
}
183189

@@ -186,6 +192,9 @@ static int fuse_xattr_set(const struct xattr_handler *handler,
186192
const char *name, const void *value, size_t size,
187193
int flags)
188194
{
195+
if (fuse_is_bad(inode))
196+
return -EIO;
197+
189198
if (!value)
190199
return fuse_removexattr(inode, name);
191200

0 commit comments

Comments
 (0)