sane security_getprocattr() API
Subsystems (4)
PATCH 1/1 sane security_getprocattr() API
fs/proc/base.c +6 -15
@@ -1558,29 +1558,20 @@ static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, size_t count, loff_t *ppos) { struct inode * inode = file->f_path.dentry->d_inode; -unsigned long page; +char *p =NULL ;ssize_t length;struct task_struct *task =get_proc_task (inode); - length = -ESRCH ;if (!task) - goto out_no_task; - -if (count >PAGE_SIZE ) - count =PAGE_SIZE ; - length = -ENOMEM ; -if (!(page =__get_free_page (GFP_KERNEL ))) - goto out; +return -ESRCH ; length =security_getprocattr (task, (char *)file->f_path.dentry->d_name.name, - (void *)page, count); -if (length >=0 ) - length =simple_read_from_buffer (buf, count, ppos, (char *)page, length); -free_page (page); -out: + &p);put_task_struct (task); -out_no_task: +if (length >0 ) + length =simple_read_from_buffer (buf, count, ppos, p, length); +kfree (p);return length; }
include/linux/security.h +4 -4
@@ -1324,7 +1324,7 @@ struct security_operations {void (*d_instantiate) (struct dentry *dentry,struct inode *inode); -int (*getprocattr)(struct task_struct *p,char *name,void *value,size_t size); +int (*getprocattr)(struct task_struct *p,char *name,char **value);int (*setprocattr)(struct task_struct *p,char *name,void *value,size_t size);int (*secid_to_secctx)(u32 secid,char **secdata,u32 *seclen);void (*release_secctx)(char *secdata,u32 seclen);
@@ -2092,9 +2092,9 @@ static inline void security_d_instantiate (struct dentry *dentry, struct inode * security_ops->d_instantiate (dentry, inode); } -static inline int security_getprocattr (struct task_struct *p,char *name,void *value,size_t size) +static inline int security_getprocattr (struct task_struct *p,char *name,char **value) { -return security_ops->getprocattr (p, name, value, size); +return security_ops->getprocattr (p, name, value); }static inline int security_setprocattr (struct task_struct *p,char *name,void *value,size_t size)
@@ -2749,7 +2749,7 @@ static inline int security_sem_semop (struct sem_array * sma,static inline void security_d_instantiate (struct dentry *dentry,struct inode *inode) { } -static inline int security_getprocattr (struct task_struct *p,char *name,void *value,size_t size) +static inline int security_getprocattr (struct task_struct *p,char *name,char **value) {return -EINVAL ; }
kernel/auditsc.c +3 -11
@@ -741,26 +741,18 @@ void audit_log_task_context(struct audit_buffer *ab)char *ctx =NULL ;ssize_t len =0 ; - len =security_getprocattr (current,"current" ,NULL ,0 ); -if (len <0 ) { + len =security_getprocattr (current,"current" , &ctx); +if (len <=0 ) {if (len != -EINVAL ) goto error_path;return ; } - ctx =kmalloc (len,GFP_KERNEL ); -if (!ctx) - goto error_path; - - len =security_getprocattr (current,"current" , ctx, len); -if (len <0 ) - goto error_path; -audit_log_format (ab," subj=%s" , ctx); +kfree (ctx);return ; error_path: -kfree (ctx);audit_panic ("error in audit_log_task_context" );return ; }
security/dummy.c +1 -1
@@ -907,7 +907,7 @@ static void dummy_d_instantiate (struct dentry *dentry, struct inode *inode)return ; } -static int dummy_getprocattr (struct task_struct *p,char *name,void *value,size_t size) +static int dummy_getprocattr (struct task_struct *p,char *name,char **value) {return -EINVAL ; }
security/selinux/hooks.c +6 -2
@@ -4468,11 +4468,12 @@ static void selinux_d_instantiate (struct dentry *dentry, struct inode *inode) }static int selinux_getprocattr (struct task_struct *p, -char *name,void *value,size_t size) +char *name,char **value) {struct task_security_struct *tsec;u32 sid;int error; +unsigned len;if (current != p) { error =task_has_perm (current, p,PROCESS__GETATTR );
@@ -4500,7 +4501,10 @@ static int selinux_getprocattr(struct task_struct *p,if (!sid)return 0 ; -return selinux_getsecurity (sid, value, size); + error =security_sid_to_context (sid, value, &len); +if (error) +return error; +return len; } static int selinux_setprocattr(struct task_struct *p,
Quoted 24 lines · Alexander Viro
kernel/auditsc.c +2
-9@@ -741,26 +741,18 @@ void audit_log_task_context(struct audit_buffer *ab)char *ctx =NULL ;ssize_t len =0 ; - len =security_getprocattr (current,"current" ,NULL ,0 ); -if (len <0 ) { + len =security_getprocattr (current,"current" , &ctx); +if (len <=0 ) {if (len != -EINVAL ) goto error_path;return ; } - ctx =kmalloc (len,GFP_KERNEL ); -if (!ctx) - goto error_path; - - len =security_getprocattr (current,"current" , ctx, len); -if (len <0 ) - goto error_path;
kernel/auditsc.c void audit_log_task_context(struct audit_buffer *ab) · line 758
Quoted 10 lines
kernel/auditsc.c +1
-2-audit_log_format (ab," subj=%s" , ctx); +kfree (ctx);return ; error_path: -kfree (ctx);audit_panic ("error in audit_log_task_context" );return ; }
The security_getprocattr API change makes sense independently of audit, but for audit, as I mentioned earlier, I think it would make more sense to use selinux_get_task_sid() and selinux_sid_to_string() instead of security_getprocattr(), particularly as audit already calls those functions for other purposes. Or if concerned about generality, audit could call security_task_getsecid() and security_secid_to_secctx(). Also avoids issues with other logic in security_getprocattr that don't make sense when used by audit, like permission checking logic (although in the current == p case, it doesn't matter) or support for attributes other than "current".
Signature · 2 lines
Stephen Smalley National Security Agency