Skip to content

Commit bd9b51e

Browse files
author
Al Viro
committed
make default ->i_fop have ->open() fail with ENXIO
As it is, default ->i_fop has NULL ->open() (along with all other methods). The only case where it matters is reopening (via procfs symlink) a file that didn't get its ->f_op from ->i_fop - anything else will have ->i_fop assigned to something sane (default would fail on read/write/ioctl/etc.). Unfortunately, such case exists - alloc_file() users, especially anon_get_file() ones. There we have tons of opened files of very different kinds sharing the same inode. As the result, attempt to reopen those via procfs succeeds and you get a descriptor you can't do anything with. Moreover, in case of sockets we set ->i_fop that will only be used on such reopen attempts - and put a failing ->open() into it to make sure those do not succeed. It would be simpler to put such ->open() into default ->i_fop and leave it unchanged both for anon inode (as we do anyway) and for socket ones. Result: * everything going through do_dentry_open() works as it used to * sock_no_open() kludge is gone * attempts to reopen anon-inode files fail as they really ought to * ditto for aio_private_file() * ditto for perfmon - this one actually tried to imitate sock_no_open() trick, but failed to set ->i_fop, so in the current tree reopens succeed and yield completely useless descriptor. Intent clearly had been to fail with -ENXIO on such reopens; now it actually does. * everything else that used alloc_file() keeps working - it has ->i_fop set for its inodes anyway Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 1f55a6e commit bd9b51e

6 files changed

Lines changed: 8 additions & 61 deletions

File tree

‎arch/ia64/kernel/perfmon.c‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,22 +2145,12 @@ pfm_close(struct inode *inode, struct file *filp)
21452145
return 0;
21462146
}
21472147

2148-
static int
2149-
pfm_no_open(struct inode *irrelevant, struct file *dontcare)
2150-
{
2151-
DPRINT(("pfm_no_open called\n"));
2152-
return -ENXIO;
2153-
}
2154-
2155-
2156-
21572148
static const struct file_operations pfm_file_ops = {
21582149
.llseek = no_llseek,
21592150
.read = pfm_read,
21602151
.write = pfm_write,
21612152
.poll = pfm_poll,
21622153
.unlocked_ioctl = pfm_ioctl,
2163-
.open = pfm_no_open, /* special open code to disallow open via /proc */
21642154
.fasync = pfm_fasync,
21652155
.release = pfm_close,
21662156
.flush = pfm_flush

‎fs/inode.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ int proc_nr_inodes(struct ctl_table *table, int write,
114114
}
115115
#endif
116116

117+
static int no_open(struct inode *inode, struct file *file)
118+
{
119+
return -ENXIO;
120+
}
121+
117122
/**
118123
* inode_init_always - perform inode structure intialisation
119124
* @sb: superblock inode belongs to
@@ -125,15 +130,15 @@ int proc_nr_inodes(struct ctl_table *table, int write,
125130
int inode_init_always(struct super_block *sb, struct inode *inode)
126131
{
127132
static const struct inode_operations empty_iops;
128-
static const struct file_operations empty_fops;
133+
static const struct file_operations no_open_fops = {.open = no_open};
129134
struct address_space *const mapping = &inode->i_data;
130135

131136
inode->i_sb = sb;
132137
inode->i_blkbits = sb->s_blocksize_bits;
133138
inode->i_flags = 0;
134139
atomic_set(&inode->i_count, 1);
135140
inode->i_op = &empty_iops;
136-
inode->i_fop = &empty_fops;
141+
inode->i_fop = &no_open_fops;
137142
inode->__i_nlink = 1;
138143
inode->i_opflags = 0;
139144
i_uid_write(inode, 0);
@@ -1801,7 +1806,7 @@ void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
18011806
} else if (S_ISFIFO(mode))
18021807
inode->i_fop = &pipefifo_fops;
18031808
else if (S_ISSOCK(mode))
1804-
inode->i_fop = &bad_sock_fops;
1809+
; /* leave it no_open_fops */
18051810
else
18061811
printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
18071812
" inode %s:%lu\n", mode, inode->i_sb->s_id,

‎include/linux/fs.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2151,7 +2151,6 @@ static inline int sb_is_blkdev_sb(struct super_block *sb)
21512151
extern int sync_filesystem(struct super_block *);
21522152
extern const struct file_operations def_blk_fops;
21532153
extern const struct file_operations def_chr_fops;
2154-
extern const struct file_operations bad_sock_fops;
21552154
#ifdef CONFIG_BLOCK
21562155
extern int ioctl_by_bdev(struct block_device *, unsigned, unsigned long);
21572156
extern int blkdev_ioctl(struct block_device *, fmode_t, unsigned, unsigned long);

‎net/Makefile‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
# Rewritten to use lists instead of if-statements.
66
#
77

8-
obj-y := nonet.o
9-
108
obj-$(CONFIG_NET) := socket.o core/
119

1210
tmp-$(CONFIG_COMPAT) := compat.o

‎net/nonet.c‎

Lines changed: 0 additions & 26 deletions
This file was deleted.

‎net/socket.c‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ unsigned int sysctl_net_busy_read __read_mostly;
113113
unsigned int sysctl_net_busy_poll __read_mostly;
114114
#endif
115115

116-
static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
117116
static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
118117
unsigned long nr_segs, loff_t pos);
119118
static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
@@ -151,7 +150,6 @@ static const struct file_operations socket_file_ops = {
151150
.compat_ioctl = compat_sock_ioctl,
152151
#endif
153152
.mmap = sock_mmap,
154-
.open = sock_no_open, /* special open code to disallow open via /proc */
155153
.release = sock_close,
156154
.fasync = sock_fasync,
157155
.sendpage = sock_sendpage,
@@ -559,23 +557,6 @@ static struct socket *sock_alloc(void)
559557
return sock;
560558
}
561559

562-
/*
563-
* In theory you can't get an open on this inode, but /proc provides
564-
* a back door. Remember to keep it shut otherwise you'll let the
565-
* creepy crawlies in.
566-
*/
567-
568-
static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
569-
{
570-
return -ENXIO;
571-
}
572-
573-
const struct file_operations bad_sock_fops = {
574-
.owner = THIS_MODULE,
575-
.open = sock_no_open,
576-
.llseek = noop_llseek,
577-
};
578-
579560
/**
580561
* sock_release - close a socket
581562
* @sock: socket to close

0 commit comments

Comments
 (0)