From: Eric Paris <eparis@redhat.com> Date: Thu, 16 Sep 2010 18:27:18 -0400 Subject: [selinux] inode_*secctx hooks to access security ctx info Message-id: <20100916182718.3360.21605.stgit@paris.rdu.redhat.com> Patchwork-id: 28268 O-Subject: [RHEL5 PATCH -v2 2/3] LSM/SELinux: inode_{get, set, notify}secctx hooks to access LSM security context information. Bugzilla: 582374 RH-Acked-by: Alexander Viro <aviro@redhat.com> RHBZ 582374 Upstream commit 1ee65e37e904b959c24404139f5752edc66319d5 This patch introduces three new hooks. The inode_getsecctx hook is used to get all relevant information from an LSM about an inode. The inode_setsecctx is used to set both the in-core and on-disk state for the inode based on a context derived from inode_getsecctx.The final hook inode_notifysecctx will notify the LSM of a change for the in-core state of the inode in question. These hooks are for use in the labeled NFS code and addresses concerns of how to set security on an inode in a multi-xattr LSM. For historical reasons Stephen Smalley's explanation of the reason for these hooks is pasted below. Quote Stephen Smalley inode_setsecctx: Change the security context of an inode. Updates the in core security context managed by the security module and invokes the fs code as needed (via __vfs_setxattr_noperm) to update any backing xattrs that represent the context. Example usage: NFS server invokes this hook to change the security context in its incore inode and on the backing file system to a value provided by the client on a SETATTR operation. inode_notifysecctx: Notify the security module of what the security context of an inode should be. Initializes the incore security context managed by the security module for this inode. Example usage: NFS client invokes this hook to initialize the security context in its incore inode to the value provided by the server for the file when the server returned the file's attributes to the client. Signed-off-by: David P. Quigley <dpquigl@tycho.nsa.gov> Acked-by: Serge Hallyn <serue@us.ibm.com> Signed-off-by: James Morris <jmorris@namei.org> diff --git a/include/linux/security.h b/include/linux/security.h index a899161..4794617 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -1174,6 +1174,41 @@ extern int mmap_min_addr_handler(struct ctl_table *table, int write, struct file * @secdata contains the security context. * @seclen contains the length of the security context. * + * @inode_notifysecctx: + * Notify the security module of what the security context of an inode + * should be. Initializes the incore security context managed by the + * security module for this inode. Example usage: NFS client invokes + * this hook to initialize the security context in its incore inode to the + * value provided by the server for the file when the server returned the + * file's attributes to the client. + * + * Must be called with inode->i_mutex locked. + * + * @inode we wish to set the security context of. + * @ctx contains the string which we wish to set in the inode. + * @ctxlen contains the length of @ctx. + * + * @inode_setsecctx: + * Change the security context of an inode. Updates the + * incore security context managed by the security module and invokes the + * fs code as needed (via __vfs_setxattr_noperm) to update any backing + * xattrs that represent the context. Example usage: NFS server invokes + * this hook to change the security context in its incore inode and on the + * backing filesystem to a value provided by the client on a SETATTR + * operation. + * + * Must be called with inode->i_mutex locked. + * + * @dentry contains the inode we wish to set the security context of. + * @ctx contains the string which we wish to set in the inode. + * @ctxlen contains the length of @ctx. + * + * @inode_getsecctx: + * Returns a string containing all relavent security context information + * + * @inode we wish to set the security context of. + * @ctx is a pointer in which to place the allocated security context. + * @ctxlen points to the place to put the length of @ctx. * This is the main security structure. */ struct security_operations { @@ -1430,7 +1465,11 @@ struct security_operations { unsigned long prot, unsigned long flags, unsigned long addr, unsigned long addr_only); int (*vm_enough_memory_mm) (struct mm_struct *mm, long pages); + int (*inode_notifysecctx)(struct inode *inode, void *ctx, u32 ctxlen); + int (*inode_setsecctx)(struct dentry *dentry, void *ctx, u32 ctxlen); + int (*inode_getsecctx)(struct inode *inode, void **ctx, u32 *ctxlen); #endif + }; /* global variables */ @@ -2172,6 +2211,21 @@ static inline void security_release_secctx(char *secdata, u32 seclen) return security_ops->release_secctx(secdata, seclen); } +static inline int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) +{ + return security_ops->inode_notifysecctx(inode, ctx, ctxlen); +} + +static inline int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) +{ + return security_ops->inode_setsecctx(dentry, ctx, ctxlen); +} + +static inline int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) +{ + return security_ops->inode_getsecctx(inode, ctx, ctxlen); +} + /* prototypes */ extern int security_init (void); extern int register_security (struct security_operations *ops); @@ -2785,6 +2839,19 @@ static inline int security_shm_shmat (struct shmid_kernel * shp, return 0; } +static inline int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) +{ + return -EOPNOTSUPP; +} +static inline int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) +{ + return -EOPNOTSUPP; +} +static inline int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) +{ + return -EOPNOTSUPP; +} + static inline int security_sem_alloc (struct sem_array *sma) { return 0; diff --git a/security/dummy.c b/security/dummy.c index a17d35e..c26bc91 100644 --- a/security/dummy.c +++ b/security/dummy.c @@ -392,6 +392,21 @@ static int dummy_inode_setsecurity(struct inode *inode, const char *name, const return -EOPNOTSUPP; } +static int dummy_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) +{ + return 0; +} + +static int dummy_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) +{ + return 0; +} + +static int dummy_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) +{ + return 0; +} + static int dummy_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) { return 0; @@ -1040,6 +1055,9 @@ void security_fixup_ops (struct security_operations *ops) set_to_dummy_if_null(ops, inode_getsecurity); set_to_dummy_if_null(ops, inode_setsecurity); set_to_dummy_if_null(ops, inode_listsecurity); + set_to_dummy_if_null(ops, inode_notifysecctx); + set_to_dummy_if_null(ops, inode_setsecctx); + set_to_dummy_if_null(ops, inode_getsecctx); set_to_dummy_if_null(ops, file_permission); set_to_dummy_if_null(ops, file_alloc_security); set_to_dummy_if_null(ops, file_free_security); diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 2e261d9..e2cc702d 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -4719,6 +4719,35 @@ static void selinux_release_secctx(char *secdata, u32 seclen) kfree(secdata); } +/* + * called with inode->i_mutex locked + */ +static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) +{ + return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0); +} + +/* + * called with inode->i_mutex locked + */ +static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) +{ + return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); +} + +static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) +{ + int len = 0; + len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, + NULL, 0, true); + if (len < 0) + return len; + *ctx = kmalloc(len, GFP_NOFS); + len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, + *ctx, len, true); + *ctxlen = len; + return 0; +} #ifdef CONFIG_KEYS static int selinux_key_alloc(struct key *k, struct task_struct *tsk, @@ -4902,6 +4931,9 @@ static struct security_operations selinux_ops = { .secid_to_secctx = selinux_secid_to_secctx, .release_secctx = selinux_release_secctx, + .inode_notifysecctx = selinux_inode_notifysecctx, + .inode_setsecctx = selinux_inode_setsecctx, + .inode_getsecctx = selinux_inode_getsecctx, .unix_stream_connect = selinux_socket_unix_stream_connect, .unix_may_send = selinux_socket_unix_may_send,