2011-01-20 19:38:33 +03:00
/* procfs files for key database enumeration
2005-04-17 02:20:36 +04:00
*
* Copyright ( C ) 2004 Red Hat , Inc . All Rights Reserved .
* Written by David Howells ( dhowells @ redhat . com )
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation ; either version
* 2 of the License , or ( at your option ) any later version .
*/
# include <linux/module.h>
# include <linux/init.h>
# include <linux/sched.h>
# include <linux/fs.h>
# include <linux/proc_fs.h>
# include <linux/seq_file.h>
# include <asm/errno.h>
# include "internal.h"
# ifdef CONFIG_KEYS_DEBUG_PROC_KEYS
static int proc_keys_open ( struct inode * inode , struct file * file ) ;
static void * proc_keys_start ( struct seq_file * p , loff_t * _pos ) ;
static void * proc_keys_next ( struct seq_file * p , void * v , loff_t * _pos ) ;
static void proc_keys_stop ( struct seq_file * p , void * v ) ;
static int proc_keys_show ( struct seq_file * m , void * v ) ;
2008-01-23 02:02:58 +03:00
static const struct seq_operations proc_keys_ops = {
2005-04-17 02:20:36 +04:00
. start = proc_keys_start ,
. next = proc_keys_next ,
. stop = proc_keys_stop ,
. show = proc_keys_show ,
} ;
2007-02-12 11:55:37 +03:00
static const struct file_operations proc_keys_fops = {
2005-04-17 02:20:36 +04:00
. open = proc_keys_open ,
. read = seq_read ,
. llseek = seq_lseek ,
. release = seq_release ,
} ;
# endif
static int proc_key_users_open ( struct inode * inode , struct file * file ) ;
static void * proc_key_users_start ( struct seq_file * p , loff_t * _pos ) ;
static void * proc_key_users_next ( struct seq_file * p , void * v , loff_t * _pos ) ;
static void proc_key_users_stop ( struct seq_file * p , void * v ) ;
static int proc_key_users_show ( struct seq_file * m , void * v ) ;
2008-01-23 02:02:58 +03:00
static const struct seq_operations proc_key_users_ops = {
2005-04-17 02:20:36 +04:00
. start = proc_key_users_start ,
. next = proc_key_users_next ,
. stop = proc_key_users_stop ,
. show = proc_key_users_show ,
} ;
2007-02-12 11:55:37 +03:00
static const struct file_operations proc_key_users_fops = {
2005-04-17 02:20:36 +04:00
. open = proc_key_users_open ,
. read = seq_read ,
. llseek = seq_lseek ,
. release = seq_release ,
} ;
/*
2011-01-20 19:38:33 +03:00
* Declare the / proc files .
2005-04-17 02:20:36 +04:00
*/
static int __init key_proc_init ( void )
{
struct proc_dir_entry * p ;
# ifdef CONFIG_KEYS_DEBUG_PROC_KEYS
2008-04-29 12:01:27 +04:00
p = proc_create ( " keys " , 0 , NULL , & proc_keys_fops ) ;
2005-04-17 02:20:36 +04:00
if ( ! p )
panic ( " Cannot create /proc/keys \n " ) ;
# endif
2008-04-29 12:01:27 +04:00
p = proc_create ( " key-users " , 0 , NULL , & proc_key_users_fops ) ;
2005-04-17 02:20:36 +04:00
if ( ! p )
panic ( " Cannot create /proc/key-users \n " ) ;
return 0 ;
2011-01-20 19:38:27 +03:00
}
2005-04-17 02:20:36 +04:00
__initcall ( key_proc_init ) ;
/*
2011-01-20 19:38:33 +03:00
* Implement " /proc/keys " to provide a list of the keys on the system that
* grant View permission to the caller .
2005-04-17 02:20:36 +04:00
*/
# ifdef CONFIG_KEYS_DEBUG_PROC_KEYS
2012-02-08 19:53:04 +04:00
static struct rb_node * key_serial_next ( struct seq_file * p , struct rb_node * n )
2009-02-27 03:28:04 +03:00
{
2012-02-08 19:53:04 +04:00
struct user_namespace * user_ns = seq_user_ns ( p ) ;
2009-09-02 12:14:05 +04:00
n = rb_next ( n ) ;
2009-02-27 03:28:04 +03:00
while ( n ) {
struct key * key = rb_entry ( n , struct key , serial_node ) ;
2012-02-08 19:53:04 +04:00
if ( kuid_has_mapping ( user_ns , key - > user - > uid ) )
2009-02-27 03:28:04 +03:00
break ;
n = rb_next ( n ) ;
}
return n ;
}
2009-09-02 12:14:05 +04:00
static int proc_keys_open ( struct inode * inode , struct file * file )
2009-02-27 03:28:04 +03:00
{
2009-09-02 12:14:05 +04:00
return seq_open ( file , & proc_keys_ops ) ;
2009-02-27 03:28:04 +03:00
}
2012-02-08 19:53:04 +04:00
static struct key * find_ge_key ( struct seq_file * p , key_serial_t id )
2009-02-27 03:28:04 +03:00
{
2012-02-08 19:53:04 +04:00
struct user_namespace * user_ns = seq_user_ns ( p ) ;
2009-09-02 12:14:05 +04:00
struct rb_node * n = key_serial_tree . rb_node ;
struct key * minkey = NULL ;
2009-02-27 03:28:04 +03:00
2009-09-02 12:14:05 +04:00
while ( n ) {
struct key * key = rb_entry ( n , struct key , serial_node ) ;
if ( id < key - > serial ) {
if ( ! minkey | | minkey - > serial > key - > serial )
minkey = key ;
n = n - > rb_left ;
} else if ( id > key - > serial ) {
n = n - > rb_right ;
} else {
minkey = key ;
break ;
}
key = NULL ;
}
2005-04-17 02:20:36 +04:00
2009-09-02 12:14:05 +04:00
if ( ! minkey )
return NULL ;
for ( ; ; ) {
2012-02-08 19:53:04 +04:00
if ( kuid_has_mapping ( user_ns , minkey - > user - > uid ) )
2009-09-02 12:14:05 +04:00
return minkey ;
n = rb_next ( & minkey - > serial_node ) ;
if ( ! n )
return NULL ;
minkey = rb_entry ( n , struct key , serial_node ) ;
}
2005-04-17 02:20:36 +04:00
}
static void * proc_keys_start ( struct seq_file * p , loff_t * _pos )
2009-06-18 16:00:05 +04:00
__acquires ( key_serial_lock )
2005-04-17 02:20:36 +04:00
{
2009-09-02 12:14:05 +04:00
key_serial_t pos = * _pos ;
struct key * key ;
2005-04-17 02:20:36 +04:00
spin_lock ( & key_serial_lock ) ;
2009-09-02 12:14:05 +04:00
if ( * _pos > INT_MAX )
return NULL ;
2012-02-08 19:53:04 +04:00
key = find_ge_key ( p , pos ) ;
2009-09-02 12:14:05 +04:00
if ( ! key )
return NULL ;
* _pos = key - > serial ;
return & key - > serial_node ;
}
2005-04-17 02:20:36 +04:00
2009-09-02 12:14:05 +04:00
static inline key_serial_t key_node_serial ( struct rb_node * n )
{
struct key * key = rb_entry ( n , struct key , serial_node ) ;
return key - > serial ;
2005-04-17 02:20:36 +04:00
}
static void * proc_keys_next ( struct seq_file * p , void * v , loff_t * _pos )
{
2009-09-02 12:14:05 +04:00
struct rb_node * n ;
2005-04-17 02:20:36 +04:00
2012-02-08 19:53:04 +04:00
n = key_serial_next ( p , v ) ;
2009-09-02 12:14:05 +04:00
if ( n )
* _pos = key_node_serial ( n ) ;
return n ;
2005-04-17 02:20:36 +04:00
}
static void proc_keys_stop ( struct seq_file * p , void * v )
2009-06-18 16:00:05 +04:00
__releases ( key_serial_lock )
2005-04-17 02:20:36 +04:00
{
spin_unlock ( & key_serial_lock ) ;
}
static int proc_keys_show ( struct seq_file * m , void * v )
{
struct rb_node * _p = v ;
struct key * key = rb_entry ( _p , struct key , serial_node ) ;
struct timespec now ;
unsigned long timo ;
2010-06-11 20:31:10 +04:00
key_ref_t key_ref , skey_ref ;
2005-04-17 02:20:36 +04:00
char xbuf [ 12 ] ;
2006-06-26 11:24:56 +04:00
int rc ;
2013-09-24 13:35:15 +04:00
struct keyring_search_context ctx = {
. index_key . type = key - > type ,
. index_key . description = key - > description ,
. cred = current_cred ( ) ,
. match = lookup_user_key_possessed ,
. match_data = key ,
. flags = ( KEYRING_SEARCH_NO_STATE_CHECK |
KEYRING_SEARCH_LOOKUP_DIRECT ) ,
} ;
2010-06-11 20:31:10 +04:00
key_ref = make_key_ref ( key , 0 ) ;
/* determine if the key is possessed by this process (a test we can
* skip if the key does not indicate the possessor can view it
*/
if ( key - > perm & KEY_POS_VIEW ) {
2013-09-24 13:35:15 +04:00
skey_ref = search_my_process_keyrings ( & ctx ) ;
2010-06-11 20:31:10 +04:00
if ( ! IS_ERR ( skey_ref ) ) {
key_ref_put ( skey_ref ) ;
key_ref = make_key_ref ( key , 1 ) ;
}
}
2006-06-26 11:24:56 +04:00
/* check whether the current task is allowed to view the key (assuming
CRED: Inaugurate COW credentials
Inaugurate copy-on-write credentials management. This uses RCU to manage the
credentials pointer in the task_struct with respect to accesses by other tasks.
A process may only modify its own credentials, and so does not need locking to
access or modify its own credentials.
A mutex (cred_replace_mutex) is added to the task_struct to control the effect
of PTRACE_ATTACHED on credential calculations, particularly with respect to
execve().
With this patch, the contents of an active credentials struct may not be
changed directly; rather a new set of credentials must be prepared, modified
and committed using something like the following sequence of events:
struct cred *new = prepare_creds();
int ret = blah(new);
if (ret < 0) {
abort_creds(new);
return ret;
}
return commit_creds(new);
There are some exceptions to this rule: the keyrings pointed to by the active
credentials may be instantiated - keyrings violate the COW rule as managing
COW keyrings is tricky, given that it is possible for a task to directly alter
the keys in a keyring in use by another task.
To help enforce this, various pointers to sets of credentials, such as those in
the task_struct, are declared const. The purpose of this is compile-time
discouragement of altering credentials through those pointers. Once a set of
credentials has been made public through one of these pointers, it may not be
modified, except under special circumstances:
(1) Its reference count may incremented and decremented.
(2) The keyrings to which it points may be modified, but not replaced.
The only safe way to modify anything else is to create a replacement and commit
using the functions described in Documentation/credentials.txt (which will be
added by a later patch).
This patch and the preceding patches have been tested with the LTP SELinux
testsuite.
This patch makes several logical sets of alteration:
(1) execve().
This now prepares and commits credentials in various places in the
security code rather than altering the current creds directly.
(2) Temporary credential overrides.
do_coredump() and sys_faccessat() now prepare their own credentials and
temporarily override the ones currently on the acting thread, whilst
preventing interference from other threads by holding cred_replace_mutex
on the thread being dumped.
This will be replaced in a future patch by something that hands down the
credentials directly to the functions being called, rather than altering
the task's objective credentials.
(3) LSM interface.
A number of functions have been changed, added or removed:
(*) security_capset_check(), ->capset_check()
(*) security_capset_set(), ->capset_set()
Removed in favour of security_capset().
(*) security_capset(), ->capset()
New. This is passed a pointer to the new creds, a pointer to the old
creds and the proposed capability sets. It should fill in the new
creds or return an error. All pointers, barring the pointer to the
new creds, are now const.
(*) security_bprm_apply_creds(), ->bprm_apply_creds()
Changed; now returns a value, which will cause the process to be
killed if it's an error.
(*) security_task_alloc(), ->task_alloc_security()
Removed in favour of security_prepare_creds().
(*) security_cred_free(), ->cred_free()
New. Free security data attached to cred->security.
(*) security_prepare_creds(), ->cred_prepare()
New. Duplicate any security data attached to cred->security.
(*) security_commit_creds(), ->cred_commit()
New. Apply any security effects for the upcoming installation of new
security by commit_creds().
(*) security_task_post_setuid(), ->task_post_setuid()
Removed in favour of security_task_fix_setuid().
(*) security_task_fix_setuid(), ->task_fix_setuid()
Fix up the proposed new credentials for setuid(). This is used by
cap_set_fix_setuid() to implicitly adjust capabilities in line with
setuid() changes. Changes are made to the new credentials, rather
than the task itself as in security_task_post_setuid().
(*) security_task_reparent_to_init(), ->task_reparent_to_init()
Removed. Instead the task being reparented to init is referred
directly to init's credentials.
NOTE! This results in the loss of some state: SELinux's osid no
longer records the sid of the thread that forked it.
(*) security_key_alloc(), ->key_alloc()
(*) security_key_permission(), ->key_permission()
Changed. These now take cred pointers rather than task pointers to
refer to the security context.
(4) sys_capset().
This has been simplified and uses less locking. The LSM functions it
calls have been merged.
(5) reparent_to_kthreadd().
This gives the current thread the same credentials as init by simply using
commit_thread() to point that way.
(6) __sigqueue_alloc() and switch_uid()
__sigqueue_alloc() can't stop the target task from changing its creds
beneath it, so this function gets a reference to the currently applicable
user_struct which it then passes into the sigqueue struct it returns if
successful.
switch_uid() is now called from commit_creds(), and possibly should be
folded into that. commit_creds() should take care of protecting
__sigqueue_alloc().
(7) [sg]et[ug]id() and co and [sg]et_current_groups.
The set functions now all use prepare_creds(), commit_creds() and
abort_creds() to build and check a new set of credentials before applying
it.
security_task_set[ug]id() is called inside the prepared section. This
guarantees that nothing else will affect the creds until we've finished.
The calling of set_dumpable() has been moved into commit_creds().
Much of the functionality of set_user() has been moved into
commit_creds().
The get functions all simply access the data directly.
(8) security_task_prctl() and cap_task_prctl().
security_task_prctl() has been modified to return -ENOSYS if it doesn't
want to handle a function, or otherwise return the return value directly
rather than through an argument.
Additionally, cap_task_prctl() now prepares a new set of credentials, even
if it doesn't end up using it.
(9) Keyrings.
A number of changes have been made to the keyrings code:
(a) switch_uid_keyring(), copy_keys(), exit_keys() and suid_keys() have
all been dropped and built in to the credentials functions directly.
They may want separating out again later.
(b) key_alloc() and search_process_keyrings() now take a cred pointer
rather than a task pointer to specify the security context.
(c) copy_creds() gives a new thread within the same thread group a new
thread keyring if its parent had one, otherwise it discards the thread
keyring.
(d) The authorisation key now points directly to the credentials to extend
the search into rather pointing to the task that carries them.
(e) Installing thread, process or session keyrings causes a new set of
credentials to be created, even though it's not strictly necessary for
process or session keyrings (they're shared).
(10) Usermode helper.
The usermode helper code now carries a cred struct pointer in its
subprocess_info struct instead of a new session keyring pointer. This set
of credentials is derived from init_cred and installed on the new process
after it has been cloned.
call_usermodehelper_setup() allocates the new credentials and
call_usermodehelper_freeinfo() discards them if they haven't been used. A
special cred function (prepare_usermodeinfo_creds()) is provided
specifically for call_usermodehelper_setup() to call.
call_usermodehelper_setkeys() adjusts the credentials to sport the
supplied keyring as the new session keyring.
(11) SELinux.
SELinux has a number of changes, in addition to those to support the LSM
interface changes mentioned above:
(a) selinux_setprocattr() no longer does its check for whether the
current ptracer can access processes with the new SID inside the lock
that covers getting the ptracer's SID. Whilst this lock ensures that
the check is done with the ptracer pinned, the result is only valid
until the lock is released, so there's no point doing it inside the
lock.
(12) is_single_threaded().
This function has been extracted from selinux_setprocattr() and put into
a file of its own in the lib/ directory as join_session_keyring() now
wants to use it too.
The code in SELinux just checked to see whether a task shared mm_structs
with other tasks (CLONE_VM), but that isn't good enough. We really want
to know if they're part of the same thread group (CLONE_THREAD).
(13) nfsd.
The NFS server daemon now has to use the COW credentials to set the
credentials it is going to use. It really needs to pass the credentials
down to the functions it calls, but it can't do that until other patches
in this series have been applied.
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: James Morris <jmorris@namei.org>
Signed-off-by: James Morris <jmorris@namei.org>
2008-11-14 02:39:23 +03:00
* non - possession )
* - the caller holds a spinlock , and thus the RCU read lock , making our
* access to __current_cred ( ) safe
*/
2013-09-24 13:35:15 +04:00
rc = key_task_permission ( key_ref , ctx . cred , KEY_VIEW ) ;
2006-06-26 11:24:56 +04:00
if ( rc < 0 )
return 0 ;
2005-04-17 02:20:36 +04:00
now = current_kernel_time ( ) ;
2005-06-24 09:00:49 +04:00
rcu_read_lock ( ) ;
2005-04-17 02:20:36 +04:00
/* come up with a suitable timeout value */
if ( key - > expiry = = 0 ) {
memcpy ( xbuf , " perm " , 5 ) ;
2009-09-02 12:14:11 +04:00
} else if ( now . tv_sec > = key - > expiry ) {
2005-04-17 02:20:36 +04:00
memcpy ( xbuf , " expd " , 5 ) ;
2009-09-02 12:14:11 +04:00
} else {
2005-04-17 02:20:36 +04:00
timo = key - > expiry - now . tv_sec ;
if ( timo < 60 )
sprintf ( xbuf , " %lus " , timo ) ;
else if ( timo < 60 * 60 )
sprintf ( xbuf , " %lum " , timo / 60 ) ;
else if ( timo < 60 * 60 * 24 )
sprintf ( xbuf , " %luh " , timo / ( 60 * 60 ) ) ;
else if ( timo < 60 * 60 * 24 * 7 )
sprintf ( xbuf , " %lud " , timo / ( 60 * 60 * 24 ) ) ;
else
sprintf ( xbuf , " %luw " , timo / ( 60 * 60 * 24 * 7 ) ) ;
}
2005-06-24 09:00:49 +04:00
# define showflag(KEY, LETTER, FLAG) \
( test_bit ( FLAG , & ( KEY ) - > flags ) ? LETTER : ' - ' )
2012-05-11 13:56:56 +04:00
seq_printf ( m , " %08x %c%c%c%c%c%c%c %5d %4s %08x %5d %5d %-9.9s " ,
2005-04-17 02:20:36 +04:00
key - > serial ,
2005-06-24 09:00:49 +04:00
showflag ( key , ' I ' , KEY_FLAG_INSTANTIATED ) ,
showflag ( key , ' R ' , KEY_FLAG_REVOKED ) ,
showflag ( key , ' D ' , KEY_FLAG_DEAD ) ,
showflag ( key , ' Q ' , KEY_FLAG_IN_QUOTA ) ,
showflag ( key , ' U ' , KEY_FLAG_USER_CONSTRUCT ) ,
showflag ( key , ' N ' , KEY_FLAG_NEGATIVE ) ,
2012-05-11 13:56:56 +04:00
showflag ( key , ' i ' , KEY_FLAG_INVALIDATED ) ,
2005-04-17 02:20:36 +04:00
atomic_read ( & key - > usage ) ,
xbuf ,
key - > perm ,
2012-02-08 19:53:04 +04:00
from_kuid_munged ( seq_user_ns ( m ) , key - > uid ) ,
from_kgid_munged ( seq_user_ns ( m ) , key - > gid ) ,
2005-04-17 02:20:36 +04:00
key - > type - > name ) ;
2005-06-24 09:00:49 +04:00
# undef showflag
2005-04-17 02:20:36 +04:00
if ( key - > type - > describe )
key - > type - > describe ( key , m ) ;
seq_putc ( m , ' \n ' ) ;
2005-06-24 09:00:49 +04:00
rcu_read_unlock ( ) ;
2005-04-17 02:20:36 +04:00
return 0 ;
}
# endif /* CONFIG_KEYS_DEBUG_PROC_KEYS */
2012-02-08 19:53:04 +04:00
static struct rb_node * __key_user_next ( struct user_namespace * user_ns , struct rb_node * n )
2009-02-27 03:28:04 +03:00
{
while ( n ) {
struct key_user * user = rb_entry ( n , struct key_user , node ) ;
2012-02-08 19:53:04 +04:00
if ( kuid_has_mapping ( user_ns , user - > uid ) )
2009-02-27 03:28:04 +03:00
break ;
n = rb_next ( n ) ;
}
return n ;
}
2012-02-08 19:53:04 +04:00
static struct rb_node * key_user_next ( struct user_namespace * user_ns , struct rb_node * n )
2009-02-27 03:28:04 +03:00
{
2012-02-08 19:53:04 +04:00
return __key_user_next ( user_ns , rb_next ( n ) ) ;
2009-02-27 03:28:04 +03:00
}
2012-02-08 19:53:04 +04:00
static struct rb_node * key_user_first ( struct user_namespace * user_ns , struct rb_root * r )
2009-02-27 03:28:04 +03:00
{
struct rb_node * n = rb_first ( r ) ;
2012-02-08 19:53:04 +04:00
return __key_user_next ( user_ns , n ) ;
2009-02-27 03:28:04 +03:00
}
2009-09-02 12:14:11 +04:00
2005-04-17 02:20:36 +04:00
/*
2011-01-20 19:38:33 +03:00
* Implement " /proc/key-users " to provides a list of the key users and their
* quotas .
2005-04-17 02:20:36 +04:00
*/
static int proc_key_users_open ( struct inode * inode , struct file * file )
{
return seq_open ( file , & proc_key_users_ops ) ;
}
static void * proc_key_users_start ( struct seq_file * p , loff_t * _pos )
2009-06-18 16:00:05 +04:00
__acquires ( key_user_lock )
2005-04-17 02:20:36 +04:00
{
struct rb_node * _p ;
loff_t pos = * _pos ;
spin_lock ( & key_user_lock ) ;
2012-02-08 19:53:04 +04:00
_p = key_user_first ( seq_user_ns ( p ) , & key_user_tree ) ;
2005-04-17 02:20:36 +04:00
while ( pos > 0 & & _p ) {
pos - - ;
2012-02-08 19:53:04 +04:00
_p = key_user_next ( seq_user_ns ( p ) , _p ) ;
2005-04-17 02:20:36 +04:00
}
return _p ;
}
static void * proc_key_users_next ( struct seq_file * p , void * v , loff_t * _pos )
{
( * _pos ) + + ;
2012-02-08 19:53:04 +04:00
return key_user_next ( seq_user_ns ( p ) , ( struct rb_node * ) v ) ;
2005-04-17 02:20:36 +04:00
}
static void proc_key_users_stop ( struct seq_file * p , void * v )
2009-06-18 16:00:05 +04:00
__releases ( key_user_lock )
2005-04-17 02:20:36 +04:00
{
spin_unlock ( & key_user_lock ) ;
}
static int proc_key_users_show ( struct seq_file * m , void * v )
{
struct rb_node * _p = v ;
struct key_user * user = rb_entry ( _p , struct key_user , node ) ;
2012-02-08 19:53:04 +04:00
unsigned maxkeys = uid_eq ( user - > uid , GLOBAL_ROOT_UID ) ?
2008-04-29 12:01:32 +04:00
key_quota_root_maxkeys : key_quota_maxkeys ;
2012-02-08 19:53:04 +04:00
unsigned maxbytes = uid_eq ( user - > uid , GLOBAL_ROOT_UID ) ?
2008-04-29 12:01:32 +04:00
key_quota_root_maxbytes : key_quota_maxbytes ;
2005-04-17 02:20:36 +04:00
seq_printf ( m , " %5u: %5d %d/%d %d/%d %d/%d \n " ,
2012-02-08 19:53:04 +04:00
from_kuid_munged ( seq_user_ns ( m ) , user - > uid ) ,
2005-04-17 02:20:36 +04:00
atomic_read ( & user - > usage ) ,
atomic_read ( & user - > nkeys ) ,
atomic_read ( & user - > nikeys ) ,
user - > qnkeys ,
2008-04-29 12:01:32 +04:00
maxkeys ,
2005-04-17 02:20:36 +04:00
user - > qnbytes ,
2008-04-29 12:01:32 +04:00
maxbytes ) ;
2005-04-17 02:20:36 +04:00
return 0 ;
}