Skip to content

Commit 6b79ccb

Browse files
Arun Raghavantorvalds
authored andcommitted
keys: allow clients to set key perms in key_create_or_update()
The key_create_or_update() function provided by the keyring code has a default set of permissions that are always applied to the key when created. This might not be desirable to all clients. Here's a patch that adds a "perm" parameter to the function to address this, which can be set to KEY_PERM_UNDEF to revert to the current behaviour. Signed-off-by: Arun Raghavan <arunsr@cse.iitk.ac.in> Signed-off-by: David Howells <dhowells@redhat.com> Cc: Satyam Sharma <ssatyam@cse.iitk.ac.in> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent da91d2e commit 6b79ccb

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

include/linux/key.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ struct key;
6767
#define KEY_OTH_SETATTR 0x00000020
6868
#define KEY_OTH_ALL 0x0000003f
6969

70+
#define KEY_PERM_UNDEF 0xffffffff
71+
7072
struct seq_file;
7173
struct user_struct;
7274
struct signal_struct;
@@ -232,6 +234,7 @@ extern key_ref_t key_create_or_update(key_ref_t keyring,
232234
const char *description,
233235
const void *payload,
234236
size_t plen,
237+
key_perm_t perm,
235238
unsigned long flags);
236239

237240
extern int key_update(key_ref_t key,

security/keys/key.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -757,11 +757,11 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref,
757757
const char *description,
758758
const void *payload,
759759
size_t plen,
760+
key_perm_t perm,
760761
unsigned long flags)
761762
{
762763
struct key_type *ktype;
763764
struct key *keyring, *key = NULL;
764-
key_perm_t perm;
765765
key_ref_t key_ref;
766766
int ret;
767767

@@ -806,15 +806,17 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref,
806806
goto found_matching_key;
807807
}
808808

809-
/* decide on the permissions we want */
810-
perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
811-
perm |= KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_LINK | KEY_USR_SETATTR;
809+
/* if the client doesn't provide, decide on the permissions we want */
810+
if (perm == KEY_PERM_UNDEF) {
811+
perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
812+
perm |= KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_LINK | KEY_USR_SETATTR;
812813

813-
if (ktype->read)
814-
perm |= KEY_POS_READ | KEY_USR_READ;
814+
if (ktype->read)
815+
perm |= KEY_POS_READ | KEY_USR_READ;
815816

816-
if (ktype == &key_type_keyring || ktype->update)
817-
perm |= KEY_USR_WRITE;
817+
if (ktype == &key_type_keyring || ktype->update)
818+
perm |= KEY_USR_WRITE;
819+
}
818820

819821
/* allocate a new key */
820822
key = key_alloc(ktype, description, current->fsuid, current->fsgid,

security/keys/keyctl.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ asmlinkage long sys_add_key(const char __user *_type,
112112
/* create or update the requested key and add it to the target
113113
* keyring */
114114
key_ref = key_create_or_update(keyring_ref, type, description,
115-
payload, plen, KEY_ALLOC_IN_QUOTA);
115+
payload, plen, KEY_PERM_UNDEF,
116+
KEY_ALLOC_IN_QUOTA);
116117
if (!IS_ERR(key_ref)) {
117118
ret = key_ref_to_ptr(key_ref)->serial;
118119
key_ref_put(key_ref);

0 commit comments

Comments
 (0)