diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 199eb3f4b275..a9662213f6c6 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -1554,6 +1554,7 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent u16 sclass; struct dentry *dentry; #define INITCONTEXTLEN 255 + char context_onstack[INITCONTEXTLEN + 1]; char *context = NULL; unsigned len = 0; int rc = 0; @@ -1624,17 +1625,10 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent } len = INITCONTEXTLEN; - context = kmalloc(len+1, GFP_NOFS); - if (!context) { - rc = -ENOMEM; - dput(dentry); - goto out; - } + context = context_onstack; context[len] = '\0'; rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); if (rc == -ERANGE) { - kfree(context); - /* Need a larger buffer. Query for the right size. */ rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0); if (rc < 0) { @@ -1657,7 +1651,8 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent pr_warn("SELinux: %s: getxattr returned " "%d for dev=%s ino=%ld\n", __func__, -rc, inode->i_sb->s_id, inode->i_ino); - kfree(context); + if (context != context_onstack) + kfree(context); goto out; } /* Map ENODATA to the default file SID */ @@ -1682,13 +1677,15 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent "returned %d for dev=%s ino=%ld\n", __func__, context, -rc, dev, ino); } - kfree(context); + if (context != context_onstack) + kfree(context); /* Leave with the unlabeled SID */ rc = 0; break; } } - kfree(context); + if (context != context_onstack) + kfree(context); break; case SECURITY_FS_USE_TASK: sid = task_sid;