Skip to contentSkip to conversation

Cleanup inode_getsecurity and callers for use with labeled NFS

RFC In review David P. Quigley 2 participants 9 messages Aug 17, 2007 – Aug 27, 2007

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

These patches modify inode_getsecurity and its callers to use the format similar to the one used by getprocattr which was proposed by Al Viro. They also rework vfs_getxattr to avoid unnecessary calls to the LSM. These patches are to support the ongoing work on Labeled NFS.

The first patch converts inode_getsecurity to return the security blob via a parameter and the length of the blob via the function's return value. This removes the need for the called to guess the length of the security blob. The patch also removed the unused err parameter. All of the callers are updated to use a helper function named xattr_getsecurity which keeps the xattr style interface and performs generic checks which were originally in the security modules.

The second patch reworks vfs_getxattr to unconditionally call to the LSM in the case that we are requesting a security namespace attribute. Originally we would grab the value from disk then clobber it with the one from the LSM. This way we can grab it directly from the LSM and in the case when there is no module loaded or it does not support inode_getsecurity we fall back on the inode getxattr operation.

David Quigley

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#1 lore raw

PATCH 1/2 VFS/Security: Rework inode_getsecurity and callers to return resulting buffer

From: David P. Quigley <[email protected]>

This patch modifies the interface to inode_getsecurity to have the function return a buffer containing the security blob and its length via parameters instead of relying on the calling function to give it an appropriately sized buffer. Security blobs obtained with this function should be freed using the release_secctx LSM hook. This alleviates the problem of the caller having to guess a length and preallocate a buffer for this function allowing it to be used elsewhere for Labeled NFS. The patch also removed the unused err parameter. The same conversion is similar to the one used by Al Viro for the security_getprocattr hook.

---
 fs/xattr.c               |   27 +++++++++++++++++++++++++--
 include/linux/security.h |   27 ++++++++++++++-------------
 include/linux/xattr.h    |    1 +
 mm/shmem.c               |    3 +--
 security/dummy.c         |    4 +++-
 security/selinux/hooks.c |   39 +++++++++++----------------------------
 6 files changed, 55 insertions(+), 46 deletions(-)
fs/xattr.c +25 -2
index a44fd92..6d59a72 100644
@@ -105,6 +105,30 @@ out:
 EXPORT_SYMBOL_GPL(vfs_setxattr);
 
 ssize_t
+xattr_getsecurity(struct inode *inode, const char *name, void *value,
+			size_t size)
+{
+	void *buffer = NULL;
+	ssize_t len;
+
+	len = security_inode_getsecurity(inode, name, &buffer);
+	if (len < 0)
+		return len;
+	if (!value || !size) {
+		goto out;
+	}
+	if (size < len) {
+		len = -ERANGE;
+		goto out;
+	}
+	memcpy(value, buffer, len);
+out:
+	security_release_secctx(buffer, len);
+	return len;
+}
+EXPORT_SYMBOL_GPL(xattr_getsecurity);
+
+ssize_t
 vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size)
 {
 	struct inode *inode = dentry->d_inode;
@@ -126,8 +150,7 @@ vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size)
 	if (!strncmp(name, XATTR_SECURITY_PREFIX,
 				XATTR_SECURITY_PREFIX_LEN)) {
 		const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
-		int ret = security_inode_getsecurity(inode, suffix, value,
-						     size, error);
+		int ret = xattr_getsecurity(inode, suffix, value, size);
 		/*
 		 * Only overwrite the return value if a security module
 		 * is actually active.
include/linux/security.h +14 -13
index c11dc8a..b6dfa0a 100644
@@ -391,15 +391,11 @@ struct request_sock;
  * 	identified by @name for @dentry.
  * 	Return 0 if permission is granted.
  * @inode_getsecurity:
- *	Copy the extended attribute representation of the security label 
- *	associated with @name for @inode into @buffer.  @buffer may be
- *	NULL to request the size of the buffer required.  @size indicates
- *	the size of @buffer in bytes.  Note that @name is the remainder
- *	of the attribute name after the security. prefix has been removed.
- *	@err is the return value from the preceding fs getxattr call,
- *	and can be used by the security module to determine whether it
- *	should try and canonicalize the attribute value.
- *	Return number of bytes used/required on success.
+ *	Retrieve a copy of the extended attribute representation of the
+ *	security label associated with @name for @inode via @buffer.  Note that
+ *	@name is the remainder of the attribute name after the security. prefix
+ *	has been removed.  
+ *	Return size of buffer on success.
  * @inode_setsecurity:
  *	Set the security label associated with @name for @inode from the
  *	extended attribute value @value.  @size indicates the size of the
@@ -1232,7 +1228,8 @@ struct security_operations {
 	int (*inode_listxattr) (struct dentry *dentry);
 	int (*inode_removexattr) (struct dentry *dentry, char *name);
 	const char *(*inode_xattr_getsuffix) (void);
-  	int (*inode_getsecurity)(const struct inode *inode, const char *name, void *buffer, size_t size, int err);
+	int (*inode_getsecurity)(const struct inode *inode, const char *name,
+					void **buffer);
   	int (*inode_setsecurity)(struct inode *inode, const char *name, const void *value, size_t size, int flags);
   	int (*inode_listsecurity)(struct inode *inode, char *buffer, size_t buffer_size);
 
@@ -1772,11 +1769,13 @@ static inline const char *security_inode_xattr_getsuffix(void)
 	return security_ops->inode_xattr_getsuffix();
 }
 
-static inline int security_inode_getsecurity(const struct inode *inode, const char *name, void *buffer, size_t size, int err)
+static inline int security_inode_getsecurity(const struct inode *inode,
+						const char *name,
+						void **buffer)
 {
 	if (unlikely (IS_PRIVATE (inode)))
 		return 0;
-	return security_ops->inode_getsecurity(inode, name, buffer, size, err);
+	return security_ops->inode_getsecurity(inode, name, buffer);
 }
 
 static inline int security_inode_setsecurity(struct inode *inode, const char *name, const void *value, size_t size, int flags)
@@ -2458,7 +2457,9 @@ static inline const char *security_inode_xattr_getsuffix (void)
 	return NULL ;
 }
 
-static inline int security_inode_getsecurity(const struct inode *inode, const char *name, void *buffer, size_t size, int err)
+static inline int security_inode_getsecurity(const struct inode *inode,
+						const char *name,
+						void **buffer)
 {
 	return -EOPNOTSUPP;
 }
include/linux/xattr.h +1 -0
index def131a..df6b95d 100644
@@ -46,6 +46,7 @@ struct xattr_handler {
 		   size_t size, int flags);
 };
 
+ssize_t xattr_getsecurity(struct inode *, const char *, void *, size_t);
 ssize_t vfs_getxattr(struct dentry *, char *, void *, size_t);
 ssize_t vfs_listxattr(struct dentry *d, char *list, size_t size);
 int vfs_setxattr(struct dentry *, char *, void *, size_t, int);
mm/shmem.c +1 -2
index fcd19d3..5ed3a66 100644
@@ -1958,8 +1958,7 @@ static int shmem_xattr_security_get(struct inode *inode, const char *name,
 {
 	if (strcmp(name, "") == 0)
 		return -EINVAL;
-	return security_inode_getsecurity(inode, name, buffer, size,
-					  -EOPNOTSUPP);
+	return xattr_getsecurity(inode, name, buffer, size);
 }
 
 static int shmem_xattr_security_set(struct inode *inode, const char *name,
security/dummy.c +3 -1
index 19d813d..b1d28b4 100644
@@ -377,7 +377,9 @@ static int dummy_inode_removexattr (struct dentry *dentry, char *name)
 	return 0;
 }
 
-static int dummy_inode_getsecurity(const struct inode *inode, const char *name, void *buffer, size_t size, int err)
+static int dummy_inode_getsecurity(const struct inode *inode,
+					const char *name,
+					void **buffer)
 {
 	return -EOPNOTSUPP;
 }
security/selinux/hooks.c +11 -28
index 6237933..bd51f74 100644
@@ -125,32 +125,6 @@ static DEFINE_SPINLOCK(sb_security_lock);
 
 static struct kmem_cache *sel_inode_cache;
 
-/* Return security context for a given sid or just the context 
-   length if the buffer is null or length is 0 */
-static int selinux_getsecurity(u32 sid, void *buffer, size_t size)
-{
-	char *context;
-	unsigned len;
-	int rc;
-
-	rc = security_sid_to_context(sid, &context, &len);
-	if (rc)
-		return rc;
-
-	if (!buffer || !size)
-		goto getsecurity_exit;
-
-	if (size < len) {
-		len = -ERANGE;
-		goto getsecurity_exit;
-	}
-	memcpy(buffer, context, len);
-
-getsecurity_exit:
-	kfree(context);
-	return len;
-}
-
 /* Allocate and free functions for each kind of security blob. */
 
 static int task_alloc_security(struct task_struct *task)
@@ -2418,14 +2392,23 @@ static const char *selinux_inode_xattr_getsuffix(void)
  *
  * Permission check is handled by selinux_inode_getxattr hook.
  */
-static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void *buffer, size_t size, int err)
+static int selinux_inode_getsecurity(const struct inode *inode,
+					const char *name,
+					void **buffer)
 {
+	u32 size;
+	int error;
 	struct inode_security_struct *isec = inode->i_security;
 
 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
 		return -EOPNOTSUPP;
 
-	return selinux_getsecurity(isec->sid, buffer, size);
+	error = security_sid_to_context(isec->sid, (char **)buffer, &size);
+	if (error)
+		return error;
+	error = size;
+	return error;
+	
 }
 
 static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Signature · 7 lines
1.5.2.2


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#2 lore raw

Re: [PATCH 1/2] VFS/Security: Rework inode_getsecurity and callers to return resulting buffer

Quoted 7 lines · --- "David P. Quigley

From: David P. Quigley <[email protected]>

This patch modifies the interface to inode_getsecurity to have the function return a buffer containing the security blob and its length via parameters instead of relying on the calling function to give it an appropriately sized buffer. Security blobs obtained with this function should be freed using the release_secctx LSM hook.

You are making the assumption that the LSM is going to return a secctx for all possible named attributes. The caller already has to know the name of the interesting attribute, that should imply that the caller has enough information to get the right size for a buffer. I don't think much of interfaces that require you to allocate memory that you're just going to throw away after you glance at the data, especially in the file system lookup path.

Quoted 5 lines

This alleviates the problem of the caller having to
guess a length and preallocate a buffer for this function allowing it to be used elsewhere for Labeled NFS. The patch also removed the unused err parameter. The same conversion is similar to the one used by Al Viro for the security_getprocattr hook.

These are very different use frequency cases.

Casey Schaufler
[email protected]

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#3 lore raw

Re: [PATCH 1/2] VFS/Security: Rework inode_getsecurity and callers to return resulting buffer

Quoted 10 lines · Casey Schaufler
Quoted 7 lines · --- "David P. Quigley

From: David P. Quigley <[email protected]>

This patch modifies the interface to inode_getsecurity to have the function return a buffer containing the security blob and its length via parameters instead of relying on the calling function to give it an appropriately sized buffer. Security blobs obtained with this function should be freed using the release_secctx LSM hook.

Quoted 4 lines

You are making the assumption that the LSM is going to return a secctx for all possible named attributes. The caller already has to know the name of the interesting attribute, that should imply that the caller has enough information to get the right size for a buffer.

But why introduce an additional call to get the information when the interface can just give it to you? Also the only named attribute that the LSM should be returning is its security information in the void * blob as your interfaces propose. Perhaps release_secctx should be renamed to release_security or security_release to make it sound more inline with these functions. In your case the implementation for this is trivial and has little to no overhead. In the case of getsecurity you just return your internal pointer. In the case of release_secctx (or other name for it) you just have a nop.

Quoted 4 lines

I don't think
much of interfaces that require you to allocate memory that you're just going to throw away after you glance at the data, especially in the file system lookup path.

Sorry but I'm having problems parsing this. However if I understand what I think your trying to say is that not many interfaces have you allocate the buffer, return a value, quickly look at it, and toss it out. This isn't really what this is used for. The idea here is to make inode_getsecurity work as a way of getting an object with an interface for managing its lifecycle from the security module. There are other places where this functionality is useful outside of the getxattr functions and to request that other parts of the kernel either perform magic guessing on the security blob length or make the call to grab the length first doesn't seem efficient to me.

Quoted 8 lines
Quoted 6 lines

This alleviates the problem of the caller having to
guess a length and preallocate a buffer for this function allowing it to be used elsewhere for Labeled NFS. The patch also removed the unused err parameter. The same conversion is similar to the one used by Al Viro for the security_getprocattr hook.

These are very different use frequency cases.

Which one are you arguing is more frequent than the other?

Quoted 5 lines

Casey Schaufler
[email protected]

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#4 lore raw

Re: [PATCH 1/2] VFS/Security: Rework inode_getsecurity and callers to return resulting buffer

In reply to Casey Schaufler
Quoted 10 lines · Casey Schaufler
Quoted 7 lines · --- "David P. Quigley

From: David P. Quigley <[email protected]>

This patch modifies the interface to inode_getsecurity to have the function return a buffer containing the security blob and its length via parameters instead of relying on the calling function to give it an appropriately sized buffer. Security blobs obtained with this function should be freed using the release_secctx LSM hook.

Quoted 7 lines

You are making the assumption that the LSM is going to return a secctx for all possible named attributes. The caller already has to know the name of the interesting attribute, that should imply that the caller has enough information to get the right size for a buffer. I don't think much of interfaces that require you to allocate memory that you're just going to throw away after you glance at the data, especially in the file system lookup path.

Ok sorry I read this a couple of more times and I understand this now. Your issue isn't a concern. This is going to be done somewhere for an lsm since it has to allocate space to store the information internally and then copy it into the buffer provided by getxattr. In your case there isn't even an allocation and freeing of data here. In SELinux we had to allocate a string to store the converted sid in and then copy it up into the buffer internally. Regardless a copy is going to happen the question is where.

Quoted 6 lines
Quoted 5 lines

This alleviates the problem of the caller having to
guess a length and preallocate a buffer for this function allowing it to be used elsewhere for Labeled NFS. The patch also removed the unused err parameter. The same conversion is similar to the one used by Al Viro for the security_getprocattr hook.

Quoted 6 lines

These are very different use frequency cases.

Casey Schaufler
[email protected]

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#5 lore raw

Re: [PATCH 1/2] VFS/Security: Rework inode_getsecurity and callers to return resulting buffer

Quoted 23 lines · --- "David P. Quigley
Quoted 5 lines · Casey Schaufler
Quoted 3 lines · --- "David P. Quigley

From: David P. Quigley <[email protected]>

This patch modifies the interface to inode_getsecurity to have the

function

Quoted 1 line
Quoted 1 line

return a buffer containing the security blob and its length via

parameters

Quoted 1 line
Quoted 1 line

instead of relying on the calling function to give it an appropriately

sized

Quoted 1 line
Quoted 1 line

buffer. Security blobs obtained with this function should be freed using

the

Quoted 9 lines
Quoted 1 line

release_secctx LSM hook.

You are making the assumption that the LSM is going to return a secctx for all possible named attributes. The caller already has to know the name of the interesting attribute, that should imply that the caller has enough information to get the right size for a buffer. I don't think much of interfaces that require you to allocate memory that you're just going to throw away after you glance at the data, especially in the file system lookup path.

Quoted 8 lines

Ok sorry I read this a couple of more times and I understand this now. Your issue isn't a concern. This is going to be done somewhere for an lsm since it has to allocate space to store the information internally and then copy it into the buffer provided by getxattr. In your case there isn't even an allocation and freeing of data here. In SELinux we had to allocate a string to store the converted sid in and then copy it up into the buffer internally. Regardless a copy is going to happen the question is where.

I checked and indeed everyone who calls this is allocating a buffer anyway (no one is using the stack) so replacing the caller allocated buffer with an LSM managed scheme ought not to be a concern.

Quoted 17 lines
Quoted 2 lines
Quoted 2 lines

This alleviates the problem of the caller having to
guess a length and preallocate a buffer for this function allowing it to

be

Quoted 2 lines
Quoted 2 lines

used elsewhere for Labeled NFS. The patch also removed the unused err parameter. The same conversion is similar to the one used by Al Viro for

the

Quoted 8 lines
Quoted 1 line

security_getprocattr hook.

These are very different use frequency cases.

Casey Schaufler
[email protected]

Casey Schaufler
[email protected]

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#6 lore raw

Re: [PATCH 1/2] VFS/Security: Rework inode_getsecurity and callers to return resulting buffer

If there are no other concerns with this I'd like to propose that it be queued up for the 2.6.24 merge window. If you want I can repost the patches.

Dave Quigley

Quoted 58 lines · Casey Schaufler
Quoted 23 lines · --- "David P. Quigley
Quoted 5 lines · Casey Schaufler
Quoted 3 lines · --- "David P. Quigley

From: David P. Quigley <[email protected]>

This patch modifies the interface to inode_getsecurity to have the

function

Quoted 1 line
Quoted 1 line

return a buffer containing the security blob and its length via

parameters

Quoted 1 line
Quoted 1 line

instead of relying on the calling function to give it an appropriately

sized

Quoted 1 line
Quoted 1 line

buffer. Security blobs obtained with this function should be freed using

the

Quoted 9 lines
Quoted 1 line

release_secctx LSM hook.

You are making the assumption that the LSM is going to return a secctx for all possible named attributes. The caller already has to know the name of the interesting attribute, that should imply that the caller has enough information to get the right size for a buffer. I don't think much of interfaces that require you to allocate memory that you're just going to throw away after you glance at the data, especially in the file system lookup path.

Quoted 8 lines

Ok sorry I read this a couple of more times and I understand this now. Your issue isn't a concern. This is going to be done somewhere for an lsm since it has to allocate space to store the information internally and then copy it into the buffer provided by getxattr. In your case there isn't even an allocation and freeing of data here. In SELinux we had to allocate a string to store the converted sid in and then copy it up into the buffer internally. Regardless a copy is going to happen the question is where.

I checked and indeed everyone who calls this is allocating a buffer anyway (no one is using the stack) so replacing the caller allocated buffer with an LSM managed scheme ought not to be a concern.

Quoted 17 lines
Quoted 2 lines
Quoted 2 lines

This alleviates the problem of the caller having to
guess a length and preallocate a buffer for this function allowing it to

be

Quoted 2 lines
Quoted 2 lines

used elsewhere for Labeled NFS. The patch also removed the unused err parameter. The same conversion is similar to the one used by Al Viro for

the

Quoted 8 lines
Quoted 1 line

security_getprocattr hook.

These are very different use frequency cases.

Casey Schaufler
[email protected]

Quoted 7 lines

Casey Schaufler
[email protected]

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#7 lore raw

Re: [PATCH 1/2] VFS/Security: Rework inode_getsecurity and callers to return resulting buffer

Quoted 3 lines · --- "David P. Quigley

If there are no other concerns with this I'd like to propose that it be queued up for the 2.6.24 merge window. If you want I can repost the patches.

My concerns were addressed by education. Proceed as you will.

Oh, and you can queue up the Smack patch while you're at it.

(Joshua says I should put a smiley on when I say things like this. :) )

Casey Schaufler
[email protected]

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#8 lore raw

PATCH 2/2 VFS: Reorder vfs_getxattr to avoid unnecessary calls to the LSM

In reply to David P. Quigley

From: David P. Quigley <[email protected]>

Originally vfs_getxattr would pull the security xattr variable using
the inode getxattr handle and then proceed to clobber it with a subsequent call to the LSM. This patch reorders the two operations such that when the xattr requested is in the security namespace it first attempts to grab the value from the LSM directly. If it fails to obtain the value because there is no module present or the module does not support the operation it will fall back to using the inode getxattr operation. In the event that both are inaccessible it returns EOPNOTSUPP.

---
 fs/xattr.c |   15 ++++++++-------
 1 files changed, 8 insertions(+), 7 deletions(-)
fs/xattr.c +8 -7
index 6d59a72..e04a030 100644
@@ -142,11 +142,6 @@ vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size)
 	if (error)
 		return error;
 
-	if (inode->i_op->getxattr)
-		error = inode->i_op->getxattr(dentry, name, value, size);
-	else
-		error = -EOPNOTSUPP;
-
 	if (!strncmp(name, XATTR_SECURITY_PREFIX,
 				XATTR_SECURITY_PREFIX_LEN)) {
 		const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
@@ -155,9 +150,15 @@ vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size)
 		 * Only overwrite the return value if a security module
 		 * is actually active.
 		 */
-		if (ret != -EOPNOTSUPP)
-			error = ret;
+		if (ret == -EOPNOTSUPP)
+			goto nolsm;
+		return ret;
 	}
+nolsm:
+	if (inode->i_op->getxattr)
+		error = inode->i_op->getxattr(dentry, name, value, size);
+	else
+		error = -EOPNOTSUPP;
 
 	return error;
 }
Signature · 7 lines
1.5.2.2


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.