diff options
author | Al Viro <viro@zeniv.linux.org.uk> | 2008-07-26 16:01:20 -0400 |
---|---|---|
committer | Al Viro <viro@zeniv.linux.org.uk> | 2008-07-26 20:53:45 -0400 |
commit | 4e1e018ecc6f7bfd10fc75b3ff9715cc8164e0a2 (patch) | |
tree | 75404b1269b079a327551f76a9b3f941f5b11a77 /fs | |
parent | 6c5d0512a091480c9f981162227fdb1c9d70e555 (diff) |
[PATCH] fix RLIM_NOFILE handling
* dup2() should return -EBADF on exceeded sysctl_nr_open
* dup() should *not* return -EINVAL even if you have rlimit set to 0;
it should get -EMFILE instead.
Check for orig_start exceeding rlimit taken to sys_fcntl().
Failing expand_files() in dup{2,3}() now gets -EMFILE remapped to -EBADF.
Consequently, remaining checks for rlimit are taken to expand_files().
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Diffstat (limited to 'fs')
-rw-r--r-- | fs/fcntl.c | 18 | ||||
-rw-r--r-- | fs/file.c | 9 | ||||
-rw-r--r-- | fs/open.c | 9 |
3 files changed, 15 insertions, 21 deletions
diff --git a/fs/fcntl.c b/fs/fcntl.c index 3deec9887089..61d625136813 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c | |||
@@ -64,11 +64,6 @@ static int locate_fd(unsigned int orig_start, int cloexec) | |||
64 | struct fdtable *fdt; | 64 | struct fdtable *fdt; |
65 | 65 | ||
66 | spin_lock(&files->file_lock); | 66 | spin_lock(&files->file_lock); |
67 | |||
68 | error = -EINVAL; | ||
69 | if (orig_start >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur) | ||
70 | goto out; | ||
71 | |||
72 | repeat: | 67 | repeat: |
73 | fdt = files_fdtable(files); | 68 | fdt = files_fdtable(files); |
74 | /* | 69 | /* |
@@ -83,10 +78,6 @@ repeat: | |||
83 | if (start < fdt->max_fds) | 78 | if (start < fdt->max_fds) |
84 | newfd = find_next_zero_bit(fdt->open_fds->fds_bits, | 79 | newfd = find_next_zero_bit(fdt->open_fds->fds_bits, |
85 | fdt->max_fds, start); | 80 | fdt->max_fds, start); |
86 | |||
87 | error = -EMFILE; | ||
88 | if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur) | ||
89 | goto out; | ||
90 | 81 | ||
91 | error = expand_files(files, newfd); | 82 | error = expand_files(files, newfd); |
92 | if (error < 0) | 83 | if (error < 0) |
@@ -141,13 +132,14 @@ asmlinkage long sys_dup3(unsigned int oldfd, unsigned int newfd, int flags) | |||
141 | spin_lock(&files->file_lock); | 132 | spin_lock(&files->file_lock); |
142 | if (!(file = fcheck(oldfd))) | 133 | if (!(file = fcheck(oldfd))) |
143 | goto out_unlock; | 134 | goto out_unlock; |
144 | if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur) | ||
145 | goto out_unlock; | ||
146 | get_file(file); /* We are now finished with oldfd */ | 135 | get_file(file); /* We are now finished with oldfd */ |
147 | 136 | ||
148 | err = expand_files(files, newfd); | 137 | err = expand_files(files, newfd); |
149 | if (err < 0) | 138 | if (unlikely(err < 0)) { |
139 | if (err == -EMFILE) | ||
140 | err = -EBADF; | ||
150 | goto out_fput; | 141 | goto out_fput; |
142 | } | ||
151 | 143 | ||
152 | /* To avoid races with open() and dup(), we will mark the fd as | 144 | /* To avoid races with open() and dup(), we will mark the fd as |
153 | * in-use in the open-file bitmap throughout the entire dup2() | 145 | * in-use in the open-file bitmap throughout the entire dup2() |
@@ -328,6 +320,8 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg, | |||
328 | switch (cmd) { | 320 | switch (cmd) { |
329 | case F_DUPFD: | 321 | case F_DUPFD: |
330 | case F_DUPFD_CLOEXEC: | 322 | case F_DUPFD_CLOEXEC: |
323 | if (arg >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur) | ||
324 | break; | ||
331 | get_file(filp); | 325 | get_file(filp); |
332 | err = dupfd(filp, arg, cmd == F_DUPFD_CLOEXEC); | 326 | err = dupfd(filp, arg, cmd == F_DUPFD_CLOEXEC); |
333 | break; | 327 | break; |
@@ -250,9 +250,18 @@ int expand_files(struct files_struct *files, int nr) | |||
250 | struct fdtable *fdt; | 250 | struct fdtable *fdt; |
251 | 251 | ||
252 | fdt = files_fdtable(files); | 252 | fdt = files_fdtable(files); |
253 | |||
254 | /* | ||
255 | * N.B. For clone tasks sharing a files structure, this test | ||
256 | * will limit the total number of files that can be opened. | ||
257 | */ | ||
258 | if (nr >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur) | ||
259 | return -EMFILE; | ||
260 | |||
253 | /* Do we need to expand? */ | 261 | /* Do we need to expand? */ |
254 | if (nr < fdt->max_fds) | 262 | if (nr < fdt->max_fds) |
255 | return 0; | 263 | return 0; |
264 | |||
256 | /* Can we expand? */ | 265 | /* Can we expand? */ |
257 | if (nr >= sysctl_nr_open) | 266 | if (nr >= sysctl_nr_open) |
258 | return -EMFILE; | 267 | return -EMFILE; |
@@ -972,7 +972,6 @@ int get_unused_fd_flags(int flags) | |||
972 | int fd, error; | 972 | int fd, error; |
973 | struct fdtable *fdt; | 973 | struct fdtable *fdt; |
974 | 974 | ||
975 | error = -EMFILE; | ||
976 | spin_lock(&files->file_lock); | 975 | spin_lock(&files->file_lock); |
977 | 976 | ||
978 | repeat: | 977 | repeat: |
@@ -980,13 +979,6 @@ repeat: | |||
980 | fd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fds, | 979 | fd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fds, |
981 | files->next_fd); | 980 | files->next_fd); |
982 | 981 | ||
983 | /* | ||
984 | * N.B. For clone tasks sharing a files structure, this test | ||
985 | * will limit the total number of files that can be opened. | ||
986 | */ | ||
987 | if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur) | ||
988 | goto out; | ||
989 | |||
990 | /* Do we need to expand the fd array or fd set? */ | 982 | /* Do we need to expand the fd array or fd set? */ |
991 | error = expand_files(files, fd); | 983 | error = expand_files(files, fd); |
992 | if (error < 0) | 984 | if (error < 0) |
@@ -997,7 +989,6 @@ repeat: | |||
997 | * If we needed to expand the fs array we | 989 | * If we needed to expand the fs array we |
998 | * might have blocked - try again. | 990 | * might have blocked - try again. |
999 | */ | 991 | */ |
1000 | error = -EMFILE; | ||
1001 | goto repeat; | 992 | goto repeat; |
1002 | } | 993 | } |
1003 | 994 | ||