Skip to content

Commit d720024

Browse files
Michael LeMayLinus Torvalds
authored andcommitted
[PATCH] selinux: add hooks for key subsystem
Introduce SELinux hooks to support the access key retention subsystem within the kernel. Incorporate new flask headers from a modified version of the SELinux reference policy, with support for the new security class representing retained keys. Extend the "key_alloc" security hook with a task parameter representing the intended ownership context for the key being allocated. Attach security information to root's default keyrings within the SELinux initialization routine. Has passed David's testsuite. Signed-off-by: Michael LeMay <mdlemay@epoch.ncsc.mil> Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: James Morris <jmorris@namei.org> Acked-by: Chris Wright <chrisw@sous-sol.org> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent f893afb commit d720024

16 files changed

Lines changed: 155 additions & 27 deletions

File tree

‎Documentation/keys.txt‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This document has the following sections:
1919
- Key overview
2020
- Key service overview
2121
- Key access permissions
22+
- SELinux support
2223
- New procfs files
2324
- Userspace system call interface
2425
- Kernel services
@@ -232,6 +233,34 @@ For changing the ownership, group ID or permissions mask, being the owner of
232233
the key or having the sysadmin capability is sufficient.
233234

234235

236+
===============
237+
SELINUX SUPPORT
238+
===============
239+
240+
The security class "key" has been added to SELinux so that mandatory access
241+
controls can be applied to keys created within various contexts. This support
242+
is preliminary, and is likely to change quite significantly in the near future.
243+
Currently, all of the basic permissions explained above are provided in SELinux
244+
as well; SE Linux is simply invoked after all basic permission checks have been
245+
performed.
246+
247+
Each key is labeled with the same context as the task to which it belongs.
248+
Typically, this is the same task that was running when the key was created.
249+
The default keyrings are handled differently, but in a way that is very
250+
intuitive:
251+
252+
(*) The user and user session keyrings that are created when the user logs in
253+
are currently labeled with the context of the login manager.
254+
255+
(*) The keyrings associated with new threads are each labeled with the context
256+
of their associated thread, and both session and process keyrings are
257+
handled similarly.
258+
259+
Note, however, that the default keyrings associated with the root user are
260+
labeled with the default kernel context, since they are created early in the
261+
boot process, before root has a chance to log in.
262+
263+
235264
================
236265
NEW PROCFS FILES
237266
================

‎include/linux/key.h‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@ extern void unregister_key_type(struct key_type *ktype);
241241

242242
extern struct key *key_alloc(struct key_type *type,
243243
const char *desc,
244-
uid_t uid, gid_t gid, key_perm_t perm,
245-
int not_in_quota);
244+
uid_t uid, gid_t gid,
245+
struct task_struct *ctx,
246+
key_perm_t perm, int not_in_quota);
246247
extern int key_payload_reserve(struct key *key, size_t datalen);
247248
extern int key_instantiate_and_link(struct key *key,
248249
const void *data,
@@ -292,7 +293,9 @@ extern int key_unlink(struct key *keyring,
292293
struct key *key);
293294

294295
extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
295-
int not_in_quota, struct key *dest);
296+
struct task_struct *ctx,
297+
int not_in_quota,
298+
struct key *dest);
296299

297300
extern int keyring_clear(struct key *keyring);
298301

@@ -313,7 +316,8 @@ extern void keyring_replace_payload(struct key *key, void *replacement);
313316
* the userspace interface
314317
*/
315318
extern struct key root_user_keyring, root_session_keyring;
316-
extern int alloc_uid_keyring(struct user_struct *user);
319+
extern int alloc_uid_keyring(struct user_struct *user,
320+
struct task_struct *ctx);
317321
extern void switch_uid_keyring(struct user_struct *new_user);
318322
extern int copy_keys(unsigned long clone_flags, struct task_struct *tsk);
319323
extern int copy_thread_group_keys(struct task_struct *tsk);
@@ -342,7 +346,7 @@ extern void key_init(void);
342346
#define make_key_ref(k) ({ NULL; })
343347
#define key_ref_to_ptr(k) ({ NULL; })
344348
#define is_key_possessed(k) 0
345-
#define alloc_uid_keyring(u) 0
349+
#define alloc_uid_keyring(u,c) 0
346350
#define switch_uid_keyring(u) do { } while(0)
347351
#define __install_session_keyring(t, k) ({ NULL; })
348352
#define copy_keys(f,t) 0
@@ -355,6 +359,10 @@ extern void key_init(void);
355359
#define key_fsgid_changed(t) do { } while(0)
356360
#define key_init() do { } while(0)
357361

362+
/* Initial keyrings */
363+
extern struct key root_user_keyring;
364+
extern struct key root_session_keyring;
365+
358366
#endif /* CONFIG_KEYS */
359367
#endif /* __KERNEL__ */
360368
#endif /* _LINUX_KEY_H */

‎include/linux/security.h‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,7 @@ struct security_operations {
13131313

13141314
/* key management security hooks */
13151315
#ifdef CONFIG_KEYS
1316-
int (*key_alloc)(struct key *key);
1316+
int (*key_alloc)(struct key *key, struct task_struct *tsk);
13171317
void (*key_free)(struct key *key);
13181318
int (*key_permission)(key_ref_t key_ref,
13191319
struct task_struct *context,
@@ -3008,9 +3008,10 @@ static inline int security_xfrm_policy_lookup(struct xfrm_policy *xp, u32 sk_sid
30083008

30093009
#ifdef CONFIG_KEYS
30103010
#ifdef CONFIG_SECURITY
3011-
static inline int security_key_alloc(struct key *key)
3011+
static inline int security_key_alloc(struct key *key,
3012+
struct task_struct *tsk)
30123013
{
3013-
return security_ops->key_alloc(key);
3014+
return security_ops->key_alloc(key, tsk);
30143015
}
30153016

30163017
static inline void security_key_free(struct key *key)
@@ -3027,7 +3028,8 @@ static inline int security_key_permission(key_ref_t key_ref,
30273028

30283029
#else
30293030

3030-
static inline int security_key_alloc(struct key *key)
3031+
static inline int security_key_alloc(struct key *key,
3032+
struct task_struct *tsk)
30313033
{
30323034
return 0;
30333035
}

‎kernel/user.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ struct user_struct * alloc_uid(uid_t uid)
148148
new->mq_bytes = 0;
149149
new->locked_shm = 0;
150150

151-
if (alloc_uid_keyring(new) < 0) {
151+
if (alloc_uid_keyring(new, current) < 0) {
152152
kmem_cache_free(uid_cachep, new);
153153
return NULL;
154154
}

‎security/dummy.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ static int dummy_setprocattr(struct task_struct *p, char *name, void *value, siz
860860
}
861861

862862
#ifdef CONFIG_KEYS
863-
static inline int dummy_key_alloc(struct key *key)
863+
static inline int dummy_key_alloc(struct key *key, struct task_struct *ctx)
864864
{
865865
return 0;
866866
}

‎security/keys/key.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ static inline void key_alloc_serial(struct key *key)
247247
* instantiate the key or discard it before returning
248248
*/
249249
struct key *key_alloc(struct key_type *type, const char *desc,
250-
uid_t uid, gid_t gid, key_perm_t perm,
251-
int not_in_quota)
250+
uid_t uid, gid_t gid, struct task_struct *ctx,
251+
key_perm_t perm, int not_in_quota)
252252
{
253253
struct key_user *user = NULL;
254254
struct key *key;
@@ -318,7 +318,7 @@ struct key *key_alloc(struct key_type *type, const char *desc,
318318
#endif
319319

320320
/* let the security module know about the key */
321-
ret = security_key_alloc(key);
321+
ret = security_key_alloc(key, ctx);
322322
if (ret < 0)
323323
goto security_error;
324324

@@ -822,7 +822,7 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref,
822822

823823
/* allocate a new key */
824824
key = key_alloc(ktype, description, current->fsuid, current->fsgid,
825-
perm, not_in_quota);
825+
current, perm, not_in_quota);
826826
if (IS_ERR(key)) {
827827
key_ref = ERR_PTR(PTR_ERR(key));
828828
goto error_3;

‎security/keys/keyring.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,14 @@ static long keyring_read(const struct key *keyring,
240240
* allocate a keyring and link into the destination keyring
241241
*/
242242
struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
243-
int not_in_quota, struct key *dest)
243+
struct task_struct *ctx, int not_in_quota,
244+
struct key *dest)
244245
{
245246
struct key *keyring;
246247
int ret;
247248

248249
keyring = key_alloc(&key_type_keyring, description,
249-
uid, gid,
250+
uid, gid, ctx,
250251
(KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
251252
not_in_quota);
252253

‎security/keys/process_keys.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ struct key root_session_keyring = {
6767
/*
6868
* allocate the keyrings to be associated with a UID
6969
*/
70-
int alloc_uid_keyring(struct user_struct *user)
70+
int alloc_uid_keyring(struct user_struct *user,
71+
struct task_struct *ctx)
7172
{
7273
struct key *uid_keyring, *session_keyring;
7374
char buf[20];
@@ -76,7 +77,7 @@ int alloc_uid_keyring(struct user_struct *user)
7677
/* concoct a default session keyring */
7778
sprintf(buf, "_uid_ses.%u", user->uid);
7879

79-
session_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0, NULL);
80+
session_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, ctx, 0, NULL);
8081
if (IS_ERR(session_keyring)) {
8182
ret = PTR_ERR(session_keyring);
8283
goto error;
@@ -86,7 +87,7 @@ int alloc_uid_keyring(struct user_struct *user)
8687
* keyring */
8788
sprintf(buf, "_uid.%u", user->uid);
8889

89-
uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0,
90+
uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, ctx, 0,
9091
session_keyring);
9192
if (IS_ERR(uid_keyring)) {
9293
key_put(session_keyring);
@@ -143,7 +144,7 @@ int install_thread_keyring(struct task_struct *tsk)
143144

144145
sprintf(buf, "_tid.%u", tsk->pid);
145146

146-
keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
147+
keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
147148
if (IS_ERR(keyring)) {
148149
ret = PTR_ERR(keyring);
149150
goto error;
@@ -177,7 +178,7 @@ int install_process_keyring(struct task_struct *tsk)
177178
if (!tsk->signal->process_keyring) {
178179
sprintf(buf, "_pid.%u", tsk->tgid);
179180

180-
keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
181+
keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
181182
if (IS_ERR(keyring)) {
182183
ret = PTR_ERR(keyring);
183184
goto error;
@@ -217,7 +218,7 @@ static int install_session_keyring(struct task_struct *tsk,
217218
if (!keyring) {
218219
sprintf(buf, "_ses.%u", tsk->tgid);
219220

220-
keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
221+
keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
221222
if (IS_ERR(keyring))
222223
return PTR_ERR(keyring);
223224
}
@@ -717,7 +718,7 @@ long join_session_keyring(const char *name)
717718
keyring = find_keyring_by_name(name, 0);
718719
if (PTR_ERR(keyring) == -ENOKEY) {
719720
/* not found - try and create a new one */
720-
keyring = keyring_alloc(name, tsk->uid, tsk->gid, 0, NULL);
721+
keyring = keyring_alloc(name, tsk->uid, tsk->gid, tsk, 0, NULL);
721722
if (IS_ERR(keyring)) {
722723
ret = PTR_ERR(keyring);
723724
goto error2;

‎security/keys/request_key.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ static int call_sbin_request_key(struct key *key,
4848
/* allocate a new session keyring */
4949
sprintf(desc, "_req.%u", key->serial);
5050

51-
keyring = keyring_alloc(desc, current->fsuid, current->fsgid, 1, NULL);
51+
keyring = keyring_alloc(desc, current->fsuid, current->fsgid,
52+
current, 1, NULL);
5253
if (IS_ERR(keyring)) {
5354
ret = PTR_ERR(keyring);
5455
goto error_alloc;
@@ -137,7 +138,8 @@ static struct key *__request_key_construction(struct key_type *type,
137138

138139
/* create a key and add it to the queue */
139140
key = key_alloc(type, description,
140-
current->fsuid, current->fsgid, KEY_POS_ALL, 0);
141+
current->fsuid, current->fsgid,
142+
current, KEY_POS_ALL, 0);
141143
if (IS_ERR(key))
142144
goto alloc_failed;
143145

‎security/keys/request_key_auth.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ struct key *request_key_auth_new(struct key *target, const char *callout_info)
148148
sprintf(desc, "%x", target->serial);
149149

150150
authkey = key_alloc(&key_type_request_key_auth, desc,
151-
current->fsuid, current->fsgid,
151+
current->fsuid, current->fsgid, current,
152152
KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
153153
KEY_USR_VIEW, 1);
154154
if (IS_ERR(authkey)) {

0 commit comments

Comments
 (0)