Skip to contentSkip to conversation

fail_function: no need to check return value of debugfs_create functions

PATCH In review Greg Kroah-Hartman 2 participants 5 messages Jan 22, 2019 – Jan 23, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fail_function: no need to check return value of debugfs_create functions

When calling debugfs functions, there is no need to ever check the return value. The function can work or not, but the code logic should never do something different based on this.

Cc Masami Hiramatsu <[email protected]>
Cc Kees Cook <[email protected]>
Cc Josef Bacik <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc "Naveen N. Rao" <[email protected]>
Cc zhong jiang <[email protected]>
---
 kernel/fail_function.c | 23 +++++------------------
 1 file changed, 5 insertions(+), 18 deletions(-)
kernel/fail_function.c +5 -18
index 17f75b545f66..afc779be5ebb 100644
@@ -152,20 +152,13 @@ static int fei_retval_get(void *data, u64 *val)
 DEFINE_DEBUGFS_ATTRIBUTE(fei_retval_ops, fei_retval_get, fei_retval_set,
 			 "%llx\n");
 
-static int fei_debugfs_add_attr(struct fei_attr *attr)
+static void fei_debugfs_add_attr(struct fei_attr *attr)
 {
 	struct dentry *dir;
 
 	dir = debugfs_create_dir(attr->kp.symbol_name, fei_debugfs_dir);
-	if (!dir)
-		return -ENOMEM;
-
-	if (!debugfs_create_file("retval", 0600, dir, attr, &fei_retval_ops)) {
-		debugfs_remove_recursive(dir);
-		return -ENOMEM;
-	}
 
-	return 0;
+	debugfs_create_file("retval", 0600, dir, attr, &fei_retval_ops);
 }
 
 static void fei_debugfs_remove_attr(struct fei_attr *attr)
@@ -306,7 +299,7 @@ static ssize_t fei_write(struct file *file, const char __user *buffer,
 
 	ret = register_kprobe(&attr->kp);
 	if (!ret)
-		ret = fei_debugfs_add_attr(attr);
+		fei_debugfs_add_attr(attr);
 	if (ret < 0)
 		fei_attr_remove(attr);
 	else {
@@ -337,19 +330,13 @@ static int __init fei_debugfs_init(void)
 		return PTR_ERR(dir);
 
 	/* injectable attribute is just a symlink of error_inject/list */
-	if (!debugfs_create_symlink("injectable", dir,
-				    "../error_injection/list"))
-		goto error;
+	debugfs_create_symlink("injectable", dir, "../error_injection/list");
 
-	if (!debugfs_create_file("inject", 0600, dir, NULL, &fei_ops))
-		goto error;
+	debugfs_create_file("inject", 0600, dir, NULL, &fei_ops);
 
 	fei_debugfs_dir = dir;
 
 	return 0;
-error:
-	debugfs_remove_recursive(dir);
-	return -ENOMEM;
 }
 
 late_initcall(fei_debugfs_init);
Signature · 1 line
2.20.1
#1 lore raw
Quoted 3 lines · On Tue, 22 Jan 2019 16:21:44 +0100 Greg Kroah-Hartman…

When calling debugfs functions, there is no need to ever check the return value. The function can work or not, but the code logic should never do something different based on this.

Ah, OK. It simplifies the code. But I have a question below,

Quoted 25 lines
Cc Masami Hiramatsu <[email protected]>
Cc Kees Cook <[email protected]>
Cc Josef Bacik <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc "Naveen N. Rao" <[email protected]>
Cc zhong jiang <[email protected]>
---
 kernel/fail_function.c | 23 +++++------------------
 1 file changed, 5 insertions(+), 18 deletions(-)
kernel/fail_function.c +1 -1
index 17f75b545f66..afc779be5ebb 100644
@@ -152,20 +152,13 @@ static int fei_retval_get(void *data, u64 *val)
 DEFINE_DEBUGFS_ATTRIBUTE(fei_retval_ops, fei_retval_get, fei_retval_set,
 			 "%llx\n");
 
-static int fei_debugfs_add_attr(struct fei_attr *attr)
+static void fei_debugfs_add_attr(struct fei_attr *attr)
 {
 	struct dentry *dir;
 

kernel/fail_function.c static int fei_retval_get(void *data, u64 *val) · line 168

Quoted 10 lines
kernel/fail_function.c +0 -8
 	dir = debugfs_create_dir(attr->kp.symbol_name, fei_debugfs_dir);
-	if (!dir)
-		return -ENOMEM;
-
-	if (!debugfs_create_file("retval", 0600, dir, attr, &fei_retval_ops)) {
-		debugfs_remove_recursive(dir);
-		return -ENOMEM;
-	}
 
-	return 0;

Don't we need to check dir here? If above debugfs_create_dir() returns NULL, it seems we will create "retval" under root directory of debugfs.

Thank you,

Quoted 38 lines
kernel/fail_function.c +4 -9
+	debugfs_create_file("retval", 0600, dir, attr, &fei_retval_ops);
 }
 
 static void fei_debugfs_remove_attr(struct fei_attr *attr)
@@ -306,7 +299,7 @@ static ssize_t fei_write(struct file *file, const char __user *buffer,
 
 	ret = register_kprobe(&attr->kp);
 	if (!ret)
-		ret = fei_debugfs_add_attr(attr);
+		fei_debugfs_add_attr(attr);
 	if (ret < 0)
 		fei_attr_remove(attr);
 	else {
@@ -337,19 +330,13 @@ static int __init fei_debugfs_init(void)
 		return PTR_ERR(dir);
 
 	/* injectable attribute is just a symlink of error_inject/list */
-	if (!debugfs_create_symlink("injectable", dir,
-				    "../error_injection/list"))
-		goto error;
+	debugfs_create_symlink("injectable", dir, "../error_injection/list");
 
-	if (!debugfs_create_file("inject", 0600, dir, NULL, &fei_ops))
-		goto error;
+	debugfs_create_file("inject", 0600, dir, NULL, &fei_ops);
 
 	fei_debugfs_dir = dir;
 
 	return 0;
-error:
-	debugfs_remove_recursive(dir);
-	return -ENOMEM;
 }
 
 late_initcall(fei_debugfs_init);
Signature · 1 line
2.20.1
Signature · 1 line
Masami Hiramatsu <[email protected]>
#2 lore raw
Quoted 43 lines · Masami Hiramatsu
Quoted 3 lines · On Tue, 22 Jan 2019 16:21:44 +0100 Greg Kroah-Hartman…

When calling debugfs functions, there is no need to ever check the return value. The function can work or not, but the code logic should never do something different based on this.

Ah, OK. It simplifies the code. But I have a question below,

Quoted 34 lines
Cc Masami Hiramatsu <[email protected]>
Cc Kees Cook <[email protected]>
Cc Josef Bacik <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc "Naveen N. Rao" <[email protected]>
Cc zhong jiang <[email protected]>
---
 kernel/fail_function.c | 23 +++++------------------
 1 file changed, 5 insertions(+), 18 deletions(-)
kernel/fail_function.c +1 -8
index 17f75b545f66..afc779be5ebb 100644
@@ -152,20 +152,13 @@ static int fei_retval_get(void *data, u64 *val)
 DEFINE_DEBUGFS_ATTRIBUTE(fei_retval_ops, fei_retval_get, fei_retval_set,
 			 "%llx\n");
 
-static int fei_debugfs_add_attr(struct fei_attr *attr)
+static void fei_debugfs_add_attr(struct fei_attr *attr)
 {
 	struct dentry *dir;
 
 	dir = debugfs_create_dir(attr->kp.symbol_name, fei_debugfs_dir);
-	if (!dir)
-		return -ENOMEM;
-
-	if (!debugfs_create_file("retval", 0600, dir, attr, &fei_retval_ops)) {
-		debugfs_remove_recursive(dir);
-		return -ENOMEM;
-	}
 
Quoted 4 lines

kernel/fail_function.c static int fei_retval_get(void *data, u64 *val) · line 168

Quoted 1 line
kernel/fail_function.c +0 -1
-	return 0;

Don't we need to check dir here? If above debugfs_create_dir() returns NULL, it seems we will create "retval" under root directory of debugfs.

If NULL is returned, your system is out of memory and worse things are about to happen :)

thanks,

greg k-h

#3 lore raw
Quoted 44 lines · Greg Kroah-Hartman
Quoted 43 lines · Masami Hiramatsu
Quoted 3 lines · On Tue, 22 Jan 2019 16:21:44 +0100 Greg Kroah-Hartman…

When calling debugfs functions, there is no need to ever check the return value. The function can work or not, but the code logic should never do something different based on this.

Ah, OK. It simplifies the code. But I have a question below,

Quoted 34 lines
Cc Masami Hiramatsu <[email protected]>
Cc Kees Cook <[email protected]>
Cc Josef Bacik <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc "Naveen N. Rao" <[email protected]>
Cc zhong jiang <[email protected]>
---
 kernel/fail_function.c | 23 +++++------------------
 1 file changed, 5 insertions(+), 18 deletions(-)
kernel/fail_function.c +1 -8
index 17f75b545f66..afc779be5ebb 100644
@@ -152,20 +152,13 @@ static int fei_retval_get(void *data, u64 *val)
 DEFINE_DEBUGFS_ATTRIBUTE(fei_retval_ops, fei_retval_get, fei_retval_set,
 			 "%llx\n");
 
-static int fei_debugfs_add_attr(struct fei_attr *attr)
+static void fei_debugfs_add_attr(struct fei_attr *attr)
 {
 	struct dentry *dir;
 
 	dir = debugfs_create_dir(attr->kp.symbol_name, fei_debugfs_dir);
-	if (!dir)
-		return -ENOMEM;
-
-	if (!debugfs_create_file("retval", 0600, dir, attr, &fei_retval_ops)) {
-		debugfs_remove_recursive(dir);
-		return -ENOMEM;
-	}
 
Quoted 7 lines
Quoted 4 lines

kernel/fail_function.c static int fei_retval_get(void *data, u64 *val) · line 168

Quoted 1 line
kernel/fail_function.c +0 -1
-	return 0;

Don't we need to check dir here? If above debugfs_create_dir() returns NULL, it seems we will create "retval" under root directory of debugfs.

If NULL is returned, your system is out of memory and worse things are about to happen :)

But you aren't the first to ask about this, I guess I should just return ENOMEM and then the follow-on files will not be created. I'll go make that change to the core of debugfs to help prevent this problem.

thanks,

greg k-h

#4 lore raw
Quoted 50 lines · On Wed, 23 Jan 2019 07:34:27 +0100 Greg Kroah-Hartman…
Quoted 49 lines · Greg Kroah-Hartman
Quoted 47 lines · Masami Hiramatsu
Quoted 3 lines · On Tue, 22 Jan 2019 16:21:44 +0100 Greg Kroah-Hartman…

When calling debugfs functions, there is no need to ever check the return value. The function can work or not, but the code logic should never do something different based on this.

Ah, OK. It simplifies the code. But I have a question below,

Quoted 25 lines
Cc Masami Hiramatsu <[email protected]>
Cc Kees Cook <[email protected]>
Cc Josef Bacik <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc "Naveen N. Rao" <[email protected]>
Cc zhong jiang <[email protected]>
---
 kernel/fail_function.c | 23 +++++------------------
 1 file changed, 5 insertions(+), 18 deletions(-)
kernel/fail_function.c +1 -1
index 17f75b545f66..afc779be5ebb 100644
@@ -152,20 +152,13 @@ static int fei_retval_get(void *data, u64 *val)
 DEFINE_DEBUGFS_ATTRIBUTE(fei_retval_ops, fei_retval_get, fei_retval_set,
 			 "%llx\n");
 
-static int fei_debugfs_add_attr(struct fei_attr *attr)
+static void fei_debugfs_add_attr(struct fei_attr *attr)
 {
 	struct dentry *dir;
 

kernel/fail_function.c static int fei_retval_get(void *data, u64 *val) · line 168

Quoted 10 lines
kernel/fail_function.c +0 -8
 	dir = debugfs_create_dir(attr->kp.symbol_name, fei_debugfs_dir);
-	if (!dir)
-		return -ENOMEM;
-
-	if (!debugfs_create_file("retval", 0600, dir, attr, &fei_retval_ops)) {
-		debugfs_remove_recursive(dir);
-		return -ENOMEM;
-	}
 
-	return 0;

Don't we need to check dir here? If above debugfs_create_dir() returns NULL, it seems we will create "retval" under root directory of debugfs.

Quoted 6 lines
Quoted 2 lines

If NULL is returned, your system is out of memory and worse things are about to happen :)

But you aren't the first to ask about this, I guess I should just return ENOMEM and then the follow-on files will not be created. I'll go make that change to the core of debugfs to help prevent this problem.

I got it, and yes, returning -ENOMEM sounds good to me, especially, in this case.

Thanks!

Quoted 4 lines

thanks,

greg k-h

Signature · 1 line
Masami Hiramatsu <[email protected]>