blob: 6284b1496c298be958621ccf0556792176a1cbab [file] [log] [blame]
David Howells468ed2b2005-10-07 15:07:38 +01001/* permission.c: key permission determination
2 *
3 * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/module.h>
David Howells29db9192005-10-30 15:02:44 -080013#include <linux/security.h>
David Howells468ed2b2005-10-07 15:07:38 +010014#include "internal.h"
15
David Howellsd84f4f92008-11-14 10:39:23 +110016/**
17 * key_task_permission - Check a key can be used
18 * @key_ref: The key to check
19 * @cred: The credentials to use
20 * @perm: The permissions to check for
21 *
22 * Check to see whether permission is granted to use a key in the desired way,
23 * but permit the security modules to override.
24 *
25 * The caller must hold either a ref on cred or must hold the RCU readlock or a
26 * spinlock.
David Howells468ed2b2005-10-07 15:07:38 +010027 */
David Howellsd84f4f92008-11-14 10:39:23 +110028int key_task_permission(const key_ref_t key_ref, const struct cred *cred,
David Howells468ed2b2005-10-07 15:07:38 +010029 key_perm_t perm)
30{
31 struct key *key;
32 key_perm_t kperm;
33 int ret;
34
35 key = key_ref_to_ptr(key_ref);
36
Serge E. Hallyn8ff3bc32009-02-26 18:27:47 -060037 if (key->user->user_ns != cred->user->user_ns)
38 goto use_other_perms;
39
David Howells468ed2b2005-10-07 15:07:38 +010040 /* use the second 8-bits of permissions for keys the caller owns */
David Howellsb6dff3e2008-11-14 10:39:16 +110041 if (key->uid == cred->fsuid) {
David Howells468ed2b2005-10-07 15:07:38 +010042 kperm = key->perm >> 16;
43 goto use_these_perms;
44 }
45
46 /* use the third 8-bits of permissions for keys the caller has a group
47 * membership in common with */
48 if (key->gid != -1 && key->perm & KEY_GRP_ALL) {
David Howellsb6dff3e2008-11-14 10:39:16 +110049 if (key->gid == cred->fsgid) {
David Howells468ed2b2005-10-07 15:07:38 +010050 kperm = key->perm >> 8;
51 goto use_these_perms;
52 }
53
David Howellsb6dff3e2008-11-14 10:39:16 +110054 ret = groups_search(cred->group_info, key->gid);
David Howells468ed2b2005-10-07 15:07:38 +010055 if (ret) {
56 kperm = key->perm >> 8;
57 goto use_these_perms;
58 }
59 }
60
Serge E. Hallyn8ff3bc32009-02-26 18:27:47 -060061use_other_perms:
62
David Howells468ed2b2005-10-07 15:07:38 +010063 /* otherwise use the least-significant 8-bits */
64 kperm = key->perm;
65
66use_these_perms:
David Howellsc69e8d92008-11-14 10:39:19 +110067
David Howells7ab501d2005-10-07 16:41:24 +010068 /* use the top 8-bits of permissions for keys the caller possesses
69 * - possessor permissions are additive with other permissions
70 */
71 if (is_key_possessed(key_ref))
72 kperm |= key->perm >> 24;
73
David Howells468ed2b2005-10-07 15:07:38 +010074 kperm = kperm & perm & KEY_ALL;
75
David Howells29db9192005-10-30 15:02:44 -080076 if (kperm != perm)
77 return -EACCES;
78
79 /* let LSM be the final arbiter */
David Howellsd84f4f92008-11-14 10:39:23 +110080 return security_key_permission(key_ref, cred, perm);
David Howellsa8b17ed2011-01-20 16:38:27 +000081}
David Howells468ed2b2005-10-07 15:07:38 +010082
83EXPORT_SYMBOL(key_task_permission);
David Howellsb5f545c2006-01-08 01:02:47 -080084
David Howellsb5f545c2006-01-08 01:02:47 -080085/*
86 * validate a key
87 */
88int key_validate(struct key *key)
89{
90 struct timespec now;
91 int ret = 0;
92
93 if (key) {
94 /* check it's still accessible */
95 ret = -EKEYREVOKED;
96 if (test_bit(KEY_FLAG_REVOKED, &key->flags) ||
97 test_bit(KEY_FLAG_DEAD, &key->flags))
98 goto error;
99
100 /* check it hasn't expired */
101 ret = 0;
102 if (key->expiry) {
103 now = current_kernel_time();
104 if (now.tv_sec >= key->expiry)
105 ret = -EKEYEXPIRED;
106 }
107 }
108
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700109error:
David Howellsb5f545c2006-01-08 01:02:47 -0800110 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000111}
David Howellsb5f545c2006-01-08 01:02:47 -0800112
113EXPORT_SYMBOL(key_validate);