aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>2019-01-23 05:28:14 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2019-01-29 15:28:35 -0500
commitff9fb72bc07705c00795ca48631f7fffe24d2c6b (patch)
tree843e8ed33a99d526773470109426188e50aed3c9
parentd88c93f090f708c18195553b352b9f205e65418f (diff)
debugfs: return error values, not NULL
When an error happens, debugfs should return an error pointer value, not NULL. This will prevent the totally theoretical error where a debugfs call fails due to lack of memory, returning NULL, and that dentry value is then passed to another debugfs call, which would end up succeeding, creating a file at the root of the debugfs tree, but would then be impossible to remove (because you can not remove the directory NULL). So, to make everyone happy, always return errors, this makes the users of debugfs much simpler (they do not have to ever check the return value), and everyone can rest easy. Reported-by: Gary R Hook <ghook@amd.com> Reported-by: Heiko Carstens <heiko.carstens@de.ibm.com> Reported-by: Masami Hiramatsu <mhiramat@kernel.org> Reported-by: Michal Hocko <mhocko@kernel.org> Reported-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Reported-by: Ulf Hansson <ulf.hansson@linaro.org> Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org> Reviewed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--fs/debugfs/inode.c39
1 files changed, 22 insertions, 17 deletions
diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c
index 41ef452c1fcf..b16f8035b1af 100644
--- a/fs/debugfs/inode.c
+++ b/fs/debugfs/inode.c
@@ -254,8 +254,8 @@ MODULE_ALIAS_FS("debugfs");
254 * @parent: a pointer to the parent dentry of the file. 254 * @parent: a pointer to the parent dentry of the file.
255 * 255 *
256 * This function will return a pointer to a dentry if it succeeds. If the file 256 * This function will return a pointer to a dentry if it succeeds. If the file
257 * doesn't exist or an error occurs, %NULL will be returned. The returned 257 * doesn't exist or an error occurs, %ERR_PTR(-ERROR) will be returned. The
258 * dentry must be passed to dput() when it is no longer needed. 258 * returned dentry must be passed to dput() when it is no longer needed.
259 * 259 *
260 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 260 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
261 * returned. 261 * returned.
@@ -265,17 +265,17 @@ struct dentry *debugfs_lookup(const char *name, struct dentry *parent)
265 struct dentry *dentry; 265 struct dentry *dentry;
266 266
267 if (IS_ERR(parent)) 267 if (IS_ERR(parent))
268 return NULL; 268 return parent;
269 269
270 if (!parent) 270 if (!parent)
271 parent = debugfs_mount->mnt_root; 271 parent = debugfs_mount->mnt_root;
272 272
273 dentry = lookup_one_len_unlocked(name, parent, strlen(name)); 273 dentry = lookup_one_len_unlocked(name, parent, strlen(name));
274 if (IS_ERR(dentry)) 274 if (IS_ERR(dentry))
275 return NULL; 275 return dentry;
276 if (!d_really_is_positive(dentry)) { 276 if (!d_really_is_positive(dentry)) {
277 dput(dentry); 277 dput(dentry);
278 return NULL; 278 return ERR_PTR(-EINVAL);
279 } 279 }
280 return dentry; 280 return dentry;
281} 281}
@@ -324,7 +324,7 @@ static struct dentry *failed_creating(struct dentry *dentry)
324 inode_unlock(d_inode(dentry->d_parent)); 324 inode_unlock(d_inode(dentry->d_parent));
325 dput(dentry); 325 dput(dentry);
326 simple_release_fs(&debugfs_mount, &debugfs_mount_count); 326 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
327 return NULL; 327 return ERR_PTR(-ENOMEM);
328} 328}
329 329
330static struct dentry *end_creating(struct dentry *dentry) 330static struct dentry *end_creating(struct dentry *dentry)
@@ -347,7 +347,7 @@ static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
347 dentry = start_creating(name, parent); 347 dentry = start_creating(name, parent);
348 348
349 if (IS_ERR(dentry)) 349 if (IS_ERR(dentry))
350 return NULL; 350 return dentry;
351 351
352 inode = debugfs_get_inode(dentry->d_sb); 352 inode = debugfs_get_inode(dentry->d_sb);
353 if (unlikely(!inode)) 353 if (unlikely(!inode))
@@ -386,7 +386,8 @@ static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
386 * This function will return a pointer to a dentry if it succeeds. This 386 * This function will return a pointer to a dentry if it succeeds. This
387 * pointer must be passed to the debugfs_remove() function when the file is 387 * pointer must be passed to the debugfs_remove() function when the file is
388 * to be removed (no automatic cleanup happens if your module is unloaded, 388 * to be removed (no automatic cleanup happens if your module is unloaded,
389 * you are responsible here.) If an error occurs, %NULL will be returned. 389 * you are responsible here.) If an error occurs, %ERR_PTR(-ERROR) will be
390 * returned.
390 * 391 *
391 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 392 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
392 * returned. 393 * returned.
@@ -464,7 +465,8 @@ EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe);
464 * This function will return a pointer to a dentry if it succeeds. This 465 * This function will return a pointer to a dentry if it succeeds. This
465 * pointer must be passed to the debugfs_remove() function when the file is 466 * pointer must be passed to the debugfs_remove() function when the file is
466 * to be removed (no automatic cleanup happens if your module is unloaded, 467 * to be removed (no automatic cleanup happens if your module is unloaded,
467 * you are responsible here.) If an error occurs, %NULL will be returned. 468 * you are responsible here.) If an error occurs, %ERR_PTR(-ERROR) will be
469 * returned.
468 * 470 *
469 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 471 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
470 * returned. 472 * returned.
@@ -495,7 +497,8 @@ EXPORT_SYMBOL_GPL(debugfs_create_file_size);
495 * This function will return a pointer to a dentry if it succeeds. This 497 * This function will return a pointer to a dentry if it succeeds. This
496 * pointer must be passed to the debugfs_remove() function when the file is 498 * pointer must be passed to the debugfs_remove() function when the file is
497 * to be removed (no automatic cleanup happens if your module is unloaded, 499 * to be removed (no automatic cleanup happens if your module is unloaded,
498 * you are responsible here.) If an error occurs, %NULL will be returned. 500 * you are responsible here.) If an error occurs, %ERR_PTR(-ERROR) will be
501 * returned.
499 * 502 *
500 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 503 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
501 * returned. 504 * returned.
@@ -506,7 +509,7 @@ struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
506 struct inode *inode; 509 struct inode *inode;
507 510
508 if (IS_ERR(dentry)) 511 if (IS_ERR(dentry))
509 return NULL; 512 return dentry;
510 513
511 inode = debugfs_get_inode(dentry->d_sb); 514 inode = debugfs_get_inode(dentry->d_sb);
512 if (unlikely(!inode)) 515 if (unlikely(!inode))
@@ -545,7 +548,7 @@ struct dentry *debugfs_create_automount(const char *name,
545 struct inode *inode; 548 struct inode *inode;
546 549
547 if (IS_ERR(dentry)) 550 if (IS_ERR(dentry))
548 return NULL; 551 return dentry;
549 552
550 inode = debugfs_get_inode(dentry->d_sb); 553 inode = debugfs_get_inode(dentry->d_sb);
551 if (unlikely(!inode)) 554 if (unlikely(!inode))
@@ -581,8 +584,8 @@ EXPORT_SYMBOL(debugfs_create_automount);
581 * This function will return a pointer to a dentry if it succeeds. This 584 * This function will return a pointer to a dentry if it succeeds. This
582 * pointer must be passed to the debugfs_remove() function when the symbolic 585 * pointer must be passed to the debugfs_remove() function when the symbolic
583 * link is to be removed (no automatic cleanup happens if your module is 586 * link is to be removed (no automatic cleanup happens if your module is
584 * unloaded, you are responsible here.) If an error occurs, %NULL will be 587 * unloaded, you are responsible here.) If an error occurs, %ERR_PTR(-ERROR)
585 * returned. 588 * will be returned.
586 * 589 *
587 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 590 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
588 * returned. 591 * returned.
@@ -594,12 +597,12 @@ struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
594 struct inode *inode; 597 struct inode *inode;
595 char *link = kstrdup(target, GFP_KERNEL); 598 char *link = kstrdup(target, GFP_KERNEL);
596 if (!link) 599 if (!link)
597 return NULL; 600 return ERR_PTR(-ENOMEM);
598 601
599 dentry = start_creating(name, parent); 602 dentry = start_creating(name, parent);
600 if (IS_ERR(dentry)) { 603 if (IS_ERR(dentry)) {
601 kfree(link); 604 kfree(link);
602 return NULL; 605 return dentry;
603 } 606 }
604 607
605 inode = debugfs_get_inode(dentry->d_sb); 608 inode = debugfs_get_inode(dentry->d_sb);
@@ -827,7 +830,9 @@ exit:
827 if (dentry && !IS_ERR(dentry)) 830 if (dentry && !IS_ERR(dentry))
828 dput(dentry); 831 dput(dentry);
829 unlock_rename(new_dir, old_dir); 832 unlock_rename(new_dir, old_dir);
830 return NULL; 833 if (IS_ERR(dentry))
834 return dentry;
835 return ERR_PTR(-EINVAL);
831} 836}
832EXPORT_SYMBOL_GPL(debugfs_rename); 837EXPORT_SYMBOL_GPL(debugfs_rename);
833 838