Skip to content

Commit 7e047ef

Browse files
dhowellsLinus Torvalds
authored andcommitted
[PATCH] keys: sort out key quota system
Add the ability for key creation to overrun the user's quota in some circumstances - notably when a session keyring is created and assigned to a process that didn't previously have one. This means it's still possible to log in, should PAM require the creation of a new session keyring, and fix an overburdened key quota. Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent f116629 commit 7e047ef

11 files changed

Lines changed: 85 additions & 51 deletions

File tree

‎include/linux/key.h‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,14 @@ extern struct key *key_alloc(struct key_type *type,
248248
const char *desc,
249249
uid_t uid, gid_t gid,
250250
struct task_struct *ctx,
251-
key_perm_t perm, int not_in_quota);
251+
key_perm_t perm,
252+
unsigned long flags);
253+
254+
255+
#define KEY_ALLOC_IN_QUOTA 0x0000 /* add to quota, reject if would overrun */
256+
#define KEY_ALLOC_QUOTA_OVERRUN 0x0001 /* add to quota, permit even if overrun */
257+
#define KEY_ALLOC_NOT_IN_QUOTA 0x0002 /* not in quota */
258+
252259
extern int key_payload_reserve(struct key *key, size_t datalen);
253260
extern int key_instantiate_and_link(struct key *key,
254261
const void *data,
@@ -285,7 +292,7 @@ extern key_ref_t key_create_or_update(key_ref_t keyring,
285292
const char *description,
286293
const void *payload,
287294
size_t plen,
288-
int not_in_quota);
295+
unsigned long flags);
289296

290297
extern int key_update(key_ref_t key,
291298
const void *payload,
@@ -299,7 +306,7 @@ extern int key_unlink(struct key *keyring,
299306

300307
extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
301308
struct task_struct *ctx,
302-
int not_in_quota,
309+
unsigned long flags,
303310
struct key *dest);
304311

305312
extern int keyring_clear(struct key *keyring);

‎include/linux/security.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ struct swap_info_struct;
862862
* Permit allocation of a key and assign security data. Note that key does
863863
* not have a serial number assigned at this point.
864864
* @key points to the key.
865+
* @flags is the allocation flags
865866
* Return 0 if permission is granted, -ve error otherwise.
866867
* @key_free:
867868
* Notification of destruction; free security data.
@@ -1324,7 +1325,7 @@ struct security_operations {
13241325

13251326
/* key management security hooks */
13261327
#ifdef CONFIG_KEYS
1327-
int (*key_alloc)(struct key *key, struct task_struct *tsk);
1328+
int (*key_alloc)(struct key *key, struct task_struct *tsk, unsigned long flags);
13281329
void (*key_free)(struct key *key);
13291330
int (*key_permission)(key_ref_t key_ref,
13301331
struct task_struct *context,
@@ -3040,9 +3041,10 @@ static inline int security_xfrm_policy_lookup(struct xfrm_policy *xp, u32 sk_sid
30403041
#ifdef CONFIG_KEYS
30413042
#ifdef CONFIG_SECURITY
30423043
static inline int security_key_alloc(struct key *key,
3043-
struct task_struct *tsk)
3044+
struct task_struct *tsk,
3045+
unsigned long flags)
30443046
{
3045-
return security_ops->key_alloc(key, tsk);
3047+
return security_ops->key_alloc(key, tsk, flags);
30463048
}
30473049

30483050
static inline void security_key_free(struct key *key)
@@ -3060,7 +3062,8 @@ static inline int security_key_permission(key_ref_t key_ref,
30603062
#else
30613063

30623064
static inline int security_key_alloc(struct key *key,
3063-
struct task_struct *tsk)
3065+
struct task_struct *tsk,
3066+
unsigned long flags)
30643067
{
30653068
return 0;
30663069
}

‎security/dummy.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,8 @@ static int dummy_setprocattr(struct task_struct *p, char *name, void *value, siz
870870
}
871871

872872
#ifdef CONFIG_KEYS
873-
static inline int dummy_key_alloc(struct key *key, struct task_struct *ctx)
873+
static inline int dummy_key_alloc(struct key *key, struct task_struct *ctx,
874+
unsigned long flags)
874875
{
875876
return 0;
876877
}

‎security/keys/internal.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ extern int install_process_keyring(struct task_struct *tsk);
9999
extern struct key *request_key_and_link(struct key_type *type,
100100
const char *description,
101101
const char *callout_info,
102-
struct key *dest_keyring);
102+
struct key *dest_keyring,
103+
unsigned long flags);
103104

104105
/*
105106
* request_key authorisation

‎security/keys/key.c‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ static inline void key_alloc_serial(struct key *key)
248248
*/
249249
struct key *key_alloc(struct key_type *type, const char *desc,
250250
uid_t uid, gid_t gid, struct task_struct *ctx,
251-
key_perm_t perm, int not_in_quota)
251+
key_perm_t perm, unsigned long flags)
252252
{
253253
struct key_user *user = NULL;
254254
struct key *key;
@@ -269,12 +269,14 @@ struct key *key_alloc(struct key_type *type, const char *desc,
269269

270270
/* check that the user's quota permits allocation of another key and
271271
* its description */
272-
if (!not_in_quota) {
272+
if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
273273
spin_lock(&user->lock);
274-
if (user->qnkeys + 1 >= KEYQUOTA_MAX_KEYS ||
275-
user->qnbytes + quotalen >= KEYQUOTA_MAX_BYTES
276-
)
277-
goto no_quota;
274+
if (!(flags & KEY_ALLOC_QUOTA_OVERRUN)) {
275+
if (user->qnkeys + 1 >= KEYQUOTA_MAX_KEYS ||
276+
user->qnbytes + quotalen >= KEYQUOTA_MAX_BYTES
277+
)
278+
goto no_quota;
279+
}
278280

279281
user->qnkeys++;
280282
user->qnbytes += quotalen;
@@ -308,7 +310,7 @@ struct key *key_alloc(struct key_type *type, const char *desc,
308310
key->payload.data = NULL;
309311
key->security = NULL;
310312

311-
if (!not_in_quota)
313+
if (!(flags & KEY_ALLOC_NOT_IN_QUOTA))
312314
key->flags |= 1 << KEY_FLAG_IN_QUOTA;
313315

314316
memset(&key->type_data, 0, sizeof(key->type_data));
@@ -318,7 +320,7 @@ struct key *key_alloc(struct key_type *type, const char *desc,
318320
#endif
319321

320322
/* let the security module know about the key */
321-
ret = security_key_alloc(key, ctx);
323+
ret = security_key_alloc(key, ctx, flags);
322324
if (ret < 0)
323325
goto security_error;
324326

@@ -332,7 +334,7 @@ struct key *key_alloc(struct key_type *type, const char *desc,
332334
security_error:
333335
kfree(key->description);
334336
kmem_cache_free(key_jar, key);
335-
if (!not_in_quota) {
337+
if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
336338
spin_lock(&user->lock);
337339
user->qnkeys--;
338340
user->qnbytes -= quotalen;
@@ -345,7 +347,7 @@ struct key *key_alloc(struct key_type *type, const char *desc,
345347
no_memory_3:
346348
kmem_cache_free(key_jar, key);
347349
no_memory_2:
348-
if (!not_in_quota) {
350+
if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
349351
spin_lock(&user->lock);
350352
user->qnkeys--;
351353
user->qnbytes -= quotalen;
@@ -761,7 +763,7 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref,
761763
const char *description,
762764
const void *payload,
763765
size_t plen,
764-
int not_in_quota)
766+
unsigned long flags)
765767
{
766768
struct key_type *ktype;
767769
struct key *keyring, *key = NULL;
@@ -822,7 +824,7 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref,
822824

823825
/* allocate a new key */
824826
key = key_alloc(ktype, description, current->fsuid, current->fsgid,
825-
current, perm, not_in_quota);
827+
current, perm, flags);
826828
if (IS_ERR(key)) {
827829
key_ref = ERR_PTR(PTR_ERR(key));
828830
goto error_3;

‎security/keys/keyctl.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ asmlinkage long sys_add_key(const char __user *_type,
102102
/* create or update the requested key and add it to the target
103103
* keyring */
104104
key_ref = key_create_or_update(keyring_ref, type, description,
105-
payload, plen, 0);
105+
payload, plen, KEY_ALLOC_IN_QUOTA);
106106
if (!IS_ERR(key_ref)) {
107107
ret = key_ref_to_ptr(key_ref)->serial;
108108
key_ref_put(key_ref);
@@ -184,7 +184,8 @@ asmlinkage long sys_request_key(const char __user *_type,
184184

185185
/* do the search */
186186
key = request_key_and_link(ktype, description, callout_info,
187-
key_ref_to_ptr(dest_ref));
187+
key_ref_to_ptr(dest_ref),
188+
KEY_ALLOC_IN_QUOTA);
188189
if (IS_ERR(key)) {
189190
ret = PTR_ERR(key);
190191
goto error5;

‎security/keys/keyring.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ 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-
struct task_struct *ctx, int not_in_quota,
243+
struct task_struct *ctx, unsigned long flags,
244244
struct key *dest)
245245
{
246246
struct key *keyring;
@@ -249,7 +249,7 @@ struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
249249
keyring = key_alloc(&key_type_keyring, description,
250250
uid, gid, ctx,
251251
(KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
252-
not_in_quota);
252+
flags);
253253

254254
if (!IS_ERR(keyring)) {
255255
ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);

‎security/keys/process_keys.c‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ int alloc_uid_keyring(struct user_struct *user,
7777
/* concoct a default session keyring */
7878
sprintf(buf, "_uid_ses.%u", user->uid);
7979

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

90-
uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, ctx, 0,
91-
session_keyring);
91+
uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, ctx,
92+
KEY_ALLOC_IN_QUOTA, session_keyring);
9293
if (IS_ERR(uid_keyring)) {
9394
key_put(session_keyring);
9495
ret = PTR_ERR(uid_keyring);
@@ -144,7 +145,8 @@ int install_thread_keyring(struct task_struct *tsk)
144145

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

147-
keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
148+
keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk,
149+
KEY_ALLOC_QUOTA_OVERRUN, NULL);
148150
if (IS_ERR(keyring)) {
149151
ret = PTR_ERR(keyring);
150152
goto error;
@@ -178,7 +180,8 @@ int install_process_keyring(struct task_struct *tsk)
178180
if (!tsk->signal->process_keyring) {
179181
sprintf(buf, "_pid.%u", tsk->tgid);
180182

181-
keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
183+
keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk,
184+
KEY_ALLOC_QUOTA_OVERRUN, NULL);
182185
if (IS_ERR(keyring)) {
183186
ret = PTR_ERR(keyring);
184187
goto error;
@@ -209,6 +212,7 @@ int install_process_keyring(struct task_struct *tsk)
209212
static int install_session_keyring(struct task_struct *tsk,
210213
struct key *keyring)
211214
{
215+
unsigned long flags;
212216
struct key *old;
213217
char buf[20];
214218

@@ -218,7 +222,12 @@ static int install_session_keyring(struct task_struct *tsk,
218222
if (!keyring) {
219223
sprintf(buf, "_ses.%u", tsk->tgid);
220224

221-
keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
225+
flags = KEY_ALLOC_QUOTA_OVERRUN;
226+
if (tsk->signal->session_keyring)
227+
flags = KEY_ALLOC_IN_QUOTA;
228+
229+
keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk,
230+
flags, NULL);
222231
if (IS_ERR(keyring))
223232
return PTR_ERR(keyring);
224233
}
@@ -728,7 +737,8 @@ long join_session_keyring(const char *name)
728737
keyring = find_keyring_by_name(name, 0);
729738
if (PTR_ERR(keyring) == -ENOKEY) {
730739
/* not found - try and create a new one */
731-
keyring = keyring_alloc(name, tsk->uid, tsk->gid, tsk, 0, NULL);
740+
keyring = keyring_alloc(name, tsk->uid, tsk->gid, tsk,
741+
KEY_ALLOC_IN_QUOTA, NULL);
732742
if (IS_ERR(keyring)) {
733743
ret = PTR_ERR(keyring);
734744
goto error2;

‎security/keys/request_key.c‎

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +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,
52-
current, 1, NULL);
51+
keyring = keyring_alloc(desc, current->fsuid, current->fsgid, current,
52+
KEY_ALLOC_QUOTA_OVERRUN, NULL);
5353
if (IS_ERR(keyring)) {
5454
ret = PTR_ERR(keyring);
5555
goto error_alloc;
@@ -126,20 +126,21 @@ static int call_sbin_request_key(struct key *key,
126126
*/
127127
static struct key *__request_key_construction(struct key_type *type,
128128
const char *description,
129-
const char *callout_info)
129+
const char *callout_info,
130+
unsigned long flags)
130131
{
131132
request_key_actor_t actor;
132133
struct key_construction cons;
133134
struct timespec now;
134135
struct key *key, *authkey;
135136
int ret, negated;
136137

137-
kenter("%s,%s,%s", type->name, description, callout_info);
138+
kenter("%s,%s,%s,%lx", type->name, description, callout_info, flags);
138139

139140
/* create a key and add it to the queue */
140141
key = key_alloc(type, description,
141-
current->fsuid, current->fsgid,
142-
current, KEY_POS_ALL, 0);
142+
current->fsuid, current->fsgid, current, KEY_POS_ALL,
143+
flags);
143144
if (IS_ERR(key))
144145
goto alloc_failed;
145146

@@ -258,15 +259,16 @@ static struct key *__request_key_construction(struct key_type *type,
258259
static struct key *request_key_construction(struct key_type *type,
259260
const char *description,
260261
struct key_user *user,
261-
const char *callout_info)
262+
const char *callout_info,
263+
unsigned long flags)
262264
{
263265
struct key_construction *pcons;
264266
struct key *key, *ckey;
265267

266268
DECLARE_WAITQUEUE(myself, current);
267269

268-
kenter("%s,%s,{%d},%s",
269-
type->name, description, user->uid, callout_info);
270+
kenter("%s,%s,{%d},%s,%lx",
271+
type->name, description, user->uid, callout_info, flags);
270272

271273
/* see if there's such a key under construction already */
272274
down_write(&key_construction_sem);
@@ -282,7 +284,8 @@ static struct key *request_key_construction(struct key_type *type,
282284
}
283285

284286
/* see about getting userspace to construct the key */
285-
key = __request_key_construction(type, description, callout_info);
287+
key = __request_key_construction(type, description, callout_info,
288+
flags);
286289
error:
287290
kleave(" = %p", key);
288291
return key;
@@ -389,14 +392,15 @@ static void request_key_link(struct key *key, struct key *dest_keyring)
389392
struct key *request_key_and_link(struct key_type *type,
390393
const char *description,
391394
const char *callout_info,
392-
struct key *dest_keyring)
395+
struct key *dest_keyring,
396+
unsigned long flags)
393397
{
394398
struct key_user *user;
395399
struct key *key;
396400
key_ref_t key_ref;
397401

398-
kenter("%s,%s,%s,%p",
399-
type->name, description, callout_info, dest_keyring);
402+
kenter("%s,%s,%s,%p,%lx",
403+
type->name, description, callout_info, dest_keyring, flags);
400404

401405
/* search all the process keyrings for a key */
402406
key_ref = search_process_keyrings(type, description, type->match,
@@ -429,7 +433,8 @@ struct key *request_key_and_link(struct key_type *type,
429433
/* ask userspace (returns NULL if it waited on a key
430434
* being constructed) */
431435
key = request_key_construction(type, description,
432-
user, callout_info);
436+
user, callout_info,
437+
flags);
433438
if (key)
434439
break;
435440

@@ -485,7 +490,8 @@ struct key *request_key(struct key_type *type,
485490
const char *description,
486491
const char *callout_info)
487492
{
488-
return request_key_and_link(type, description, callout_info, NULL);
493+
return request_key_and_link(type, description, callout_info, NULL,
494+
KEY_ALLOC_IN_QUOTA);
489495

490496
} /* end request_key() */
491497

0 commit comments

Comments
 (0)