aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2008-05-19 19:37:45 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2008-05-19 19:37:45 -0400
commite23a5f66877d32f21a2ac15a200ad4a2b4c8b0ee (patch)
treef1eafaf4796abd3289fdc3384f124046f752b9d6 /fs
parentc9091f9e571386992c8c5badcec84d49753b9df1 (diff)
parente9baf6e59842285bcf9570f5094e4c27674a0f7c (diff)
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6
* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6: [PATCH] return to old errno choice in mkdir() et.al. [Patch] fs/binfmt_elf.c: fix wrong return values [PATCH] get rid of leak in compat_execve() [Patch] fs/binfmt_elf.c: fix a wrong free [PATCH] avoid multiplication overflows and signedness issues for max_fds [PATCH] dup_fd() part 4 - race fix [PATCH] dup_fd() - part 3 [PATCH] dup_fd() part 2 [PATCH] dup_fd() fixes, part 1 [PATCH] take init_files to fs/file.c
Diffstat (limited to 'fs')
-rw-r--r--fs/binfmt_elf.c9
-rw-r--r--fs/compat.c4
-rw-r--r--fs/exec.c12
-rw-r--r--fs/file.c152
-rw-r--r--fs/namei.c12
5 files changed, 173 insertions, 16 deletions
diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index b25707fee2cc..0fa95b198e6e 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -256,7 +256,7 @@ create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
256 return -EFAULT; 256 return -EFAULT;
257 len = strnlen_user((void __user *)p, MAX_ARG_STRLEN); 257 len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
258 if (!len || len > MAX_ARG_STRLEN) 258 if (!len || len > MAX_ARG_STRLEN)
259 return 0; 259 return -EINVAL;
260 p += len; 260 p += len;
261 } 261 }
262 if (__put_user(0, argv)) 262 if (__put_user(0, argv))
@@ -268,7 +268,7 @@ create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
268 return -EFAULT; 268 return -EFAULT;
269 len = strnlen_user((void __user *)p, MAX_ARG_STRLEN); 269 len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
270 if (!len || len > MAX_ARG_STRLEN) 270 if (!len || len > MAX_ARG_STRLEN)
271 return 0; 271 return -EINVAL;
272 p += len; 272 p += len;
273 } 273 }
274 if (__put_user(0, envp)) 274 if (__put_user(0, envp))
@@ -1900,7 +1900,7 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
1900 /* alloc memory for large data structures: too large to be on stack */ 1900 /* alloc memory for large data structures: too large to be on stack */
1901 elf = kmalloc(sizeof(*elf), GFP_KERNEL); 1901 elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1902 if (!elf) 1902 if (!elf)
1903 goto cleanup; 1903 goto out;
1904 1904
1905 segs = current->mm->map_count; 1905 segs = current->mm->map_count;
1906#ifdef ELF_CORE_EXTRA_PHDRS 1906#ifdef ELF_CORE_EXTRA_PHDRS
@@ -2034,8 +2034,9 @@ end_coredump:
2034 set_fs(fs); 2034 set_fs(fs);
2035 2035
2036cleanup: 2036cleanup:
2037 kfree(elf);
2038 free_note_info(&info); 2037 free_note_info(&info);
2038 kfree(elf);
2039out:
2039 return has_dumped; 2040 return has_dumped;
2040} 2041}
2041 2042
diff --git a/fs/compat.c b/fs/compat.c
index 332a869d2c53..ed43e17a5dc6 100644
--- a/fs/compat.c
+++ b/fs/compat.c
@@ -1405,7 +1405,7 @@ int compat_do_execve(char * filename,
1405 /* execve success */ 1405 /* execve success */
1406 security_bprm_free(bprm); 1406 security_bprm_free(bprm);
1407 acct_update_integrals(current); 1407 acct_update_integrals(current);
1408 kfree(bprm); 1408 free_bprm(bprm);
1409 return retval; 1409 return retval;
1410 } 1410 }
1411 1411
@@ -1424,7 +1424,7 @@ out_file:
1424 } 1424 }
1425 1425
1426out_kfree: 1426out_kfree:
1427 kfree(bprm); 1427 free_bprm(bprm);
1428 1428
1429out_ret: 1429out_ret:
1430 return retval; 1430 return retval;
diff --git a/fs/exec.c b/fs/exec.c
index 1f8a24aa1f8b..3c2ba7ce11d4 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1251,6 +1251,12 @@ int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1251 1251
1252EXPORT_SYMBOL(search_binary_handler); 1252EXPORT_SYMBOL(search_binary_handler);
1253 1253
1254void free_bprm(struct linux_binprm *bprm)
1255{
1256 free_arg_pages(bprm);
1257 kfree(bprm);
1258}
1259
1254/* 1260/*
1255 * sys_execve() executes a new program. 1261 * sys_execve() executes a new program.
1256 */ 1262 */
@@ -1320,17 +1326,15 @@ int do_execve(char * filename,
1320 retval = search_binary_handler(bprm,regs); 1326 retval = search_binary_handler(bprm,regs);
1321 if (retval >= 0) { 1327 if (retval >= 0) {
1322 /* execve success */ 1328 /* execve success */
1323 free_arg_pages(bprm);
1324 security_bprm_free(bprm); 1329 security_bprm_free(bprm);
1325 acct_update_integrals(current); 1330 acct_update_integrals(current);
1326 kfree(bprm); 1331 free_bprm(bprm);
1327 if (displaced) 1332 if (displaced)
1328 put_files_struct(displaced); 1333 put_files_struct(displaced);
1329 return retval; 1334 return retval;
1330 } 1335 }
1331 1336
1332out: 1337out:
1333 free_arg_pages(bprm);
1334 if (bprm->security) 1338 if (bprm->security)
1335 security_bprm_free(bprm); 1339 security_bprm_free(bprm);
1336 1340
@@ -1344,7 +1348,7 @@ out_file:
1344 fput(bprm->file); 1348 fput(bprm->file);
1345 } 1349 }
1346out_kfree: 1350out_kfree:
1347 kfree(bprm); 1351 free_bprm(bprm);
1348 1352
1349out_files: 1353out_files:
1350 if (displaced) 1354 if (displaced)
diff --git a/fs/file.c b/fs/file.c
index 4c6f0ea12c41..7b3887e054d0 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -26,6 +26,8 @@ struct fdtable_defer {
26}; 26};
27 27
28int sysctl_nr_open __read_mostly = 1024*1024; 28int sysctl_nr_open __read_mostly = 1024*1024;
29int sysctl_nr_open_min = BITS_PER_LONG;
30int sysctl_nr_open_max = 1024 * 1024; /* raised later */
29 31
30/* 32/*
31 * We use this list to defer free fdtables that have vmalloced 33 * We use this list to defer free fdtables that have vmalloced
@@ -119,8 +121,6 @@ static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
119 unsigned int cpy, set; 121 unsigned int cpy, set;
120 122
121 BUG_ON(nfdt->max_fds < ofdt->max_fds); 123 BUG_ON(nfdt->max_fds < ofdt->max_fds);
122 if (ofdt->max_fds == 0)
123 return;
124 124
125 cpy = ofdt->max_fds * sizeof(struct file *); 125 cpy = ofdt->max_fds * sizeof(struct file *);
126 set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *); 126 set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
@@ -261,6 +261,139 @@ int expand_files(struct files_struct *files, int nr)
261 return expand_fdtable(files, nr); 261 return expand_fdtable(files, nr);
262} 262}
263 263
264static int count_open_files(struct fdtable *fdt)
265{
266 int size = fdt->max_fds;
267 int i;
268
269 /* Find the last open fd */
270 for (i = size/(8*sizeof(long)); i > 0; ) {
271 if (fdt->open_fds->fds_bits[--i])
272 break;
273 }
274 i = (i+1) * 8 * sizeof(long);
275 return i;
276}
277
278/*
279 * Allocate a new files structure and copy contents from the
280 * passed in files structure.
281 * errorp will be valid only when the returned files_struct is NULL.
282 */
283struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
284{
285 struct files_struct *newf;
286 struct file **old_fds, **new_fds;
287 int open_files, size, i;
288 struct fdtable *old_fdt, *new_fdt;
289
290 *errorp = -ENOMEM;
291 newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
292 if (!newf)
293 goto out;
294
295 atomic_set(&newf->count, 1);
296
297 spin_lock_init(&newf->file_lock);
298 newf->next_fd = 0;
299 new_fdt = &newf->fdtab;
300 new_fdt->max_fds = NR_OPEN_DEFAULT;
301 new_fdt->close_on_exec = (fd_set *)&newf->close_on_exec_init;
302 new_fdt->open_fds = (fd_set *)&newf->open_fds_init;
303 new_fdt->fd = &newf->fd_array[0];
304 INIT_RCU_HEAD(&new_fdt->rcu);
305 new_fdt->next = NULL;
306
307 spin_lock(&oldf->file_lock);
308 old_fdt = files_fdtable(oldf);
309 open_files = count_open_files(old_fdt);
310
311 /*
312 * Check whether we need to allocate a larger fd array and fd set.
313 */
314 while (unlikely(open_files > new_fdt->max_fds)) {
315 spin_unlock(&oldf->file_lock);
316
317 if (new_fdt != &newf->fdtab) {
318 free_fdarr(new_fdt);
319 free_fdset(new_fdt);
320 kfree(new_fdt);
321 }
322
323 new_fdt = alloc_fdtable(open_files - 1);
324 if (!new_fdt) {
325 *errorp = -ENOMEM;
326 goto out_release;
327 }
328
329 /* beyond sysctl_nr_open; nothing to do */
330 if (unlikely(new_fdt->max_fds < open_files)) {
331 free_fdarr(new_fdt);
332 free_fdset(new_fdt);
333 kfree(new_fdt);
334 *errorp = -EMFILE;
335 goto out_release;
336 }
337
338 /*
339 * Reacquire the oldf lock and a pointer to its fd table
340 * who knows it may have a new bigger fd table. We need
341 * the latest pointer.
342 */
343 spin_lock(&oldf->file_lock);
344 old_fdt = files_fdtable(oldf);
345 open_files = count_open_files(old_fdt);
346 }
347
348 old_fds = old_fdt->fd;
349 new_fds = new_fdt->fd;
350
351 memcpy(new_fdt->open_fds->fds_bits,
352 old_fdt->open_fds->fds_bits, open_files/8);
353 memcpy(new_fdt->close_on_exec->fds_bits,
354 old_fdt->close_on_exec->fds_bits, open_files/8);
355
356 for (i = open_files; i != 0; i--) {
357 struct file *f = *old_fds++;
358 if (f) {
359 get_file(f);
360 } else {
361 /*
362 * The fd may be claimed in the fd bitmap but not yet
363 * instantiated in the files array if a sibling thread
364 * is partway through open(). So make sure that this
365 * fd is available to the new process.
366 */
367 FD_CLR(open_files - i, new_fdt->open_fds);
368 }
369 rcu_assign_pointer(*new_fds++, f);
370 }
371 spin_unlock(&oldf->file_lock);
372
373 /* compute the remainder to be cleared */
374 size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
375
376 /* This is long word aligned thus could use a optimized version */
377 memset(new_fds, 0, size);
378
379 if (new_fdt->max_fds > open_files) {
380 int left = (new_fdt->max_fds-open_files)/8;
381 int start = open_files / (8 * sizeof(unsigned long));
382
383 memset(&new_fdt->open_fds->fds_bits[start], 0, left);
384 memset(&new_fdt->close_on_exec->fds_bits[start], 0, left);
385 }
386
387 rcu_assign_pointer(newf->fdt, new_fdt);
388
389 return newf;
390
391out_release:
392 kmem_cache_free(files_cachep, newf);
393out:
394 return NULL;
395}
396
264static void __devinit fdtable_defer_list_init(int cpu) 397static void __devinit fdtable_defer_list_init(int cpu)
265{ 398{
266 struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu); 399 struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
@@ -274,4 +407,19 @@ void __init files_defer_init(void)
274 int i; 407 int i;
275 for_each_possible_cpu(i) 408 for_each_possible_cpu(i)
276 fdtable_defer_list_init(i); 409 fdtable_defer_list_init(i);
410 sysctl_nr_open_max = min((size_t)INT_MAX, ~(size_t)0/sizeof(void *)) &
411 -BITS_PER_LONG;
277} 412}
413
414struct files_struct init_files = {
415 .count = ATOMIC_INIT(1),
416 .fdt = &init_files.fdtab,
417 .fdtab = {
418 .max_fds = NR_OPEN_DEFAULT,
419 .fd = &init_files.fd_array[0],
420 .close_on_exec = (fd_set *)&init_files.close_on_exec_init,
421 .open_fds = (fd_set *)&init_files.open_fds_init,
422 .rcu = RCU_HEAD_INIT,
423 },
424 .file_lock = __SPIN_LOCK_UNLOCKED(init_task.file_lock),
425};
diff --git a/fs/namei.c b/fs/namei.c
index 32fd9655485b..c7e43536c49a 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -2003,18 +2003,22 @@ struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2003 if (IS_ERR(dentry)) 2003 if (IS_ERR(dentry))
2004 goto fail; 2004 goto fail;
2005 2005
2006 if (dentry->d_inode)
2007 goto eexist;
2006 /* 2008 /*
2007 * Special case - lookup gave negative, but... we had foo/bar/ 2009 * Special case - lookup gave negative, but... we had foo/bar/
2008 * From the vfs_mknod() POV we just have a negative dentry - 2010 * From the vfs_mknod() POV we just have a negative dentry -
2009 * all is fine. Let's be bastards - you had / on the end, you've 2011 * all is fine. Let's be bastards - you had / on the end, you've
2010 * been asking for (non-existent) directory. -ENOENT for you. 2012 * been asking for (non-existent) directory. -ENOENT for you.
2011 */ 2013 */
2012 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode) 2014 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2013 goto enoent; 2015 dput(dentry);
2016 dentry = ERR_PTR(-ENOENT);
2017 }
2014 return dentry; 2018 return dentry;
2015enoent: 2019eexist:
2016 dput(dentry); 2020 dput(dentry);
2017 dentry = ERR_PTR(-ENOENT); 2021 dentry = ERR_PTR(-EEXIST);
2018fail: 2022fail:
2019 return dentry; 2023 return dentry;
2020} 2024}