diff mbox series

[v1,1/9] landlock: Fix memory allocation error handling

Message ID 20201111213442.434639-2-mic@digikod.net (mailing list archive)
State New
Headers show
Series Landlock fixes | expand

Commit Message

Mickaël Salaün Nov. 11, 2020, 9:34 p.m. UTC
Handle memory allocation errors in landlock_create_object() call.  This
prevent to inadvertently hold an inode.  Also, make get_inode_object()
more readable.

Cc: James Morris <jmorris@namei.org>
Cc: Jann Horn <jannh@google.com>
Cc: Serge E. Hallyn <serge@hallyn.com>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
---
 security/landlock/fs.c     | 5 +++++
 security/landlock/object.c | 5 +++--
 2 files changed, 8 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/security/landlock/fs.c b/security/landlock/fs.c
index d8c5d19ac2af..b67c821bb40b 100644
--- a/security/landlock/fs.c
+++ b/security/landlock/fs.c
@@ -9,6 +9,7 @@ 
 #include <linux/atomic.h>
 #include <linux/compiler_types.h>
 #include <linux/dcache.h>
+#include <linux/err.h>
 #include <linux/fs.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
@@ -98,6 +99,8 @@  static struct landlock_object *get_inode_object(struct inode *const inode)
 	 * holding any locks).
 	 */
 	new_object = landlock_create_object(&landlock_fs_underops, inode);
+	if (IS_ERR(new_object))
+		return new_object;
 
 	spin_lock(&inode->i_lock);
 	object = rcu_dereference_protected(inode_sec->object,
@@ -145,6 +148,8 @@  int landlock_append_fs_rule(struct landlock_ruleset *const ruleset,
 	access_rights |= _LANDLOCK_ACCESS_FS_MASK & ~ruleset->fs_access_mask;
 	rule.access = access_rights;
 	rule.object = get_inode_object(d_backing_inode(path->dentry));
+	if (IS_ERR(rule.object))
+		return PTR_ERR(rule.object);
 	mutex_lock(&ruleset->lock);
 	err = landlock_insert_rule(ruleset, &rule, false);
 	mutex_unlock(&ruleset->lock);
diff --git a/security/landlock/object.c b/security/landlock/object.c
index a71644ee72a7..54ba0327002a 100644
--- a/security/landlock/object.c
+++ b/security/landlock/object.c
@@ -8,6 +8,7 @@ 
 
 #include <linux/bug.h>
 #include <linux/compiler_types.h>
+#include <linux/err.h>
 #include <linux/kernel.h>
 #include <linux/rcupdate.h>
 #include <linux/refcount.h>
@@ -23,10 +24,10 @@  struct landlock_object *landlock_create_object(
 	struct landlock_object *new_object;
 
 	if (WARN_ON_ONCE(!underops || !underobj))
-		return NULL;
+		return ERR_PTR(-ENOENT);
 	new_object = kzalloc(sizeof(*new_object), GFP_KERNEL_ACCOUNT);
 	if (!new_object)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 	refcount_set(&new_object->usage, 1);
 	spin_lock_init(&new_object->lock);
 	new_object->underops = underops;