Linux: Fix return codes from setpag
[openafs.git] / src / afs / LINUX / osi_groups.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * Implements:
12  * setgroups (syscall)
13  * setpag
14  *
15  */
16 #include <afsconfig.h>
17 #include "afs/param.h"
18 #ifdef LINUX_KEYRING_SUPPORT
19 #include <linux/seq_file.h>
20 #endif
21
22
23 #include "afs/sysincludes.h"
24 #include "afsincludes.h"
25 #include "afs/afs_stats.h"      /* statistics */
26 #include "afs/nfsclient.h"
27 #include "osi_compat.h"
28
29 #include <linux/smp_lock.h>
30
31 #ifdef AFS_LINUX26_ONEGROUP_ENV
32 # define NUMPAGGROUPS 1
33
34 static afs_uint32
35 afs_linux_pag_from_groups(struct group_info *group_info) {
36     afs_uint32 g0 = 0;
37     afs_uint32 i;
38
39     if (group_info->ngroups < NUMPAGGROUPS)
40         return NOPAG;
41
42     for (i = 0; (i < group_info->ngroups &&
43                  (g0 = GROUP_AT(group_info, i)) != (gid_t) NOGROUP); i++) {
44         if (((g0 >> 24) & 0xff) == 'A')
45             return g0;
46     }
47     return NOPAG;
48 }
49
50 static inline void
51 afs_linux_pag_to_groups(afs_uint32 newpag,
52                         struct group_info *old, struct group_info **new) {
53     int need_space = 0;
54     int i;
55     int j;
56
57     if (afs_linux_pag_from_groups(old) == NOPAG)
58         need_space = NUMPAGGROUPS;
59
60     *new = groups_alloc(old->ngroups + need_space);
61
62     for (i = 0, j = 0; i < old->ngroups; ++i) {
63         int ths = GROUP_AT(old, i);
64         int last = i > 0 ? GROUP_AT(old, i-1) : 0;
65         if ((ths >> 24) == 'A')
66             continue;
67         if (last <= newpag && ths > newpag) {
68            GROUP_AT(*new, j) = newpag;
69            j++;
70         }
71         GROUP_AT(*new, j) = ths;
72         j++;
73     }
74     if (j != i + need_space)
75         GROUP_AT(*new, j) = newpag;
76 }
77
78 #else
79 # define NUMPAGGROUPS 2
80
81 static inline afs_uint32
82 afs_linux_pag_from_groups(struct group_info *group_info) {
83
84     if (group_info->ngroups < NUMPAGGROUPS)
85         return NOPAG;
86
87     return afs_get_pag_from_groups(GROUP_AT(group_info, 0), GROUP_AT(group_info, 1));
88 }
89
90 static inline void
91 afs_linux_pag_to_groups(afs_uint32 newpag,
92                         struct group_info *old, struct group_info *new) {
93     int need_space = 0;
94     int i;
95     gid_t g0;
96     gid_t g1;
97
98     if (afs_linux_pag_from_groups(old) == NOPAG)
99         need_space = NUMPAGGGROUPS;
100
101     *new = groups_alloc(old->ngroups + need_space);
102
103     for (i = 0; i < old->ngroups; ++i)
104           GROUP_AT(new, i + need_space) = GROUP_AT(old, i);
105
106     afs_get_groups_from_pag(newpag, &g0, g1);
107     GROUP_AT(new, 0) = g0;
108     GROUP_AT(new, 1) = g1;
109 }
110 #endif
111
112 afs_int32
113 osi_get_group_pag(afs_ucred_t *cred) {
114     return afs_linux_pag_from_groups(afs_cr_group_info(cred));
115 }
116
117
118 static int
119 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
120 {
121     struct group_info *old_info;
122
123     AFS_STATCNT(afs_setgroups);
124
125     old_info = afs_cr_group_info(*cr);
126     get_group_info(group_info);
127     afs_set_cr_group_info(*cr, group_info);
128     put_group_info(old_info);
129
130     crset(*cr);
131
132 #if defined(STRUCT_TASK_STRUCT_HAS_PARENT) && !defined(STRUCT_TASK_STRUCT_HAS_CRED)
133     if (change_parent) {
134         old_info = current->parent->group_info;
135         get_group_info(group_info);
136         current->parent->group_info = group_info;
137         put_group_info(old_info);
138     }
139 #endif
140
141     return (0);
142 }
143
144 int
145 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
146          int change_parent)
147 {
148     struct group_info *group_info;
149     struct group_info *tmp;
150
151     get_group_info(afs_cr_group_info(*cr));
152     group_info = afs_cr_group_info(*cr);
153
154     *newpag = (pagvalue == -1 ? genpag() : pagvalue);
155     afs_linux_pag_to_groups(*newpag, group_info, &tmp);
156
157     put_group_info(group_info);
158     group_info = tmp;
159
160     afs_setgroups(cr, group_info, change_parent);
161
162     put_group_info(group_info);
163
164     return 0;
165 }
166
167 #ifdef LINUX_KEYRING_SUPPORT
168 extern struct key_type key_type_keyring __attribute__((weak));
169 static struct key_type *__key_type_keyring = &key_type_keyring;
170
171 /* install_session_keyring returns negative error values */
172 static int
173 install_session_keyring(struct key *keyring)
174 {
175     struct key *old;
176     char desc[20];
177     int code = -EINVAL;
178     unsigned long flags;
179
180     if (!__key_type_keyring)
181         return code;
182
183     if (!keyring) {
184
185         /* create an empty session keyring */
186         sprintf(desc, "_ses.%u", current->tgid);
187
188         /* if we're root, don't count the keyring against our quota. This
189          * avoids starvation issues when dealing with PAM modules that always
190          * setpag() as root */
191         if (current_uid() == 0)
192             flags = KEY_ALLOC_NOT_IN_QUOTA;
193         else
194             flags = KEY_ALLOC_IN_QUOTA;
195
196         keyring = afs_linux_key_alloc(
197                             __key_type_keyring, desc,
198                             current_uid(), current_gid(),
199                             (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
200                             flags);
201
202         if (IS_ERR(keyring)) {
203             code = PTR_ERR(keyring);
204             goto out;
205         }
206     }
207
208     code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
209     if (code < 0) {
210         key_put(keyring);
211         goto out;
212     }
213
214     /* install the keyring */
215     spin_lock_irq(&current->sighand->siglock);
216     old = task_session_keyring(current);
217     smp_wmb();
218     task_session_keyring(current) = keyring;
219     spin_unlock_irq(&current->sighand->siglock);
220
221     if (old)
222             key_put(old);
223
224 out:
225     return code;
226 }
227 #endif /* LINUX_KEYRING_SUPPORT */
228
229 /* Error codes from setpag must be positive, otherwise they don't
230  * make it back into userspace properly. Error codes from the
231  * Linux keyring utilities, and from install_session_keyring()
232  * are negative. So we need to be careful to convert them correctly
233  * here
234  */
235 int
236 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
237        int change_parent)
238 {
239     int code;
240
241     AFS_STATCNT(setpag);
242
243     code = __setpag(cr, pagvalue, newpag, change_parent);
244
245 #ifdef LINUX_KEYRING_SUPPORT
246     if (code == 0 && afs_cr_rgid(*cr) != NFSXLATOR_CRED) {
247         code = install_session_keyring(NULL);
248         if (code == 0 && current_session_keyring()) {
249             struct key *key;
250             key_perm_t perm;
251
252             perm = KEY_POS_VIEW | KEY_POS_SEARCH;
253             perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
254
255             key = afs_linux_key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, KEY_ALLOC_NOT_IN_QUOTA);
256
257             if (!IS_ERR(key)) {
258                 key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
259                                          current_session_keyring(), NULL);
260                 key_put(key);
261             } else {
262                 code = PTR_ERR(key);
263             }
264         }
265         if (code)
266             code = -code;
267     }
268 #endif /* LINUX_KEYRING_SUPPORT */
269
270     return code;
271 }
272
273
274 /* Intercept the standard system call. */
275 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
276 asmlinkage long
277 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
278 {
279     long code;
280     cred_t *cr = crref();
281     afs_uint32 junk;
282     int old_pag;
283
284     old_pag = PagInCred(cr);
285     crfree(cr);
286
287     code = (*sys_setgroupsp) (gidsetsize, grouplist);
288     if (code) {
289         return code;
290     }
291
292     cr = crref();
293     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
294         /* re-install old pag if there's room. */
295         code = __setpag(&cr, old_pag, &junk, 0);
296     }
297     crfree(cr);
298
299     /* Linux syscall ABI returns errno as negative */
300     return (-code);
301 }
302
303 /* Intercept the standard uid32 system call. */
304 extern asmlinkage int (*sys_setgroups32p) (int gidsetsize,
305                                            __kernel_gid32_t * grouplist);
306 asmlinkage long
307 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
308 {
309     long code;
310     cred_t *cr = crref();
311     afs_uint32 junk;
312     int old_pag;
313
314     old_pag = PagInCred(cr);
315     crfree(cr);
316
317     code = (*sys_setgroups32p) (gidsetsize, grouplist);
318
319     if (code) {
320         return code;
321     }
322
323     cr = crref();
324     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
325         /* re-install old pag if there's room. */
326         code = __setpag(&cr, old_pag, &junk, 0);
327     }
328     crfree(cr);
329
330     /* Linux syscall ABI returns errno as negative */
331     return (-code);
332 }
333
334 #if defined(AFS_PPC64_LINUX20_ENV)
335 /* Intercept the uid16 system call as used by 32bit programs. */
336 extern asmlinkage long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
337 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
338 {
339     long code;
340     cred_t *cr = crref();
341     afs_uint32 junk;
342     int old_pag;
343     
344     old_pag = PagInCred(cr);
345     crfree(cr);
346     
347     code = (*sys32_setgroupsp)(gidsetsize, grouplist);
348     if (code) {
349         return code;
350     }
351     
352     cr = crref();
353     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
354         /* re-install old pag if there's room. */
355         code = __setpag(&cr, old_pag, &junk, 0);
356     }
357     crfree(cr);
358     
359     /* Linux syscall ABI returns errno as negative */
360     return (-code);
361 }
362 #endif
363
364 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
365 /* Intercept the uid16 system call as used by 32bit programs. */
366 #ifdef AFS_AMD64_LINUX20_ENV
367 extern asmlinkage long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
368 #endif /* AFS_AMD64_LINUX20_ENV */
369 #ifdef AFS_SPARC64_LINUX26_ENV
370 extern asmlinkage int (*sys32_setgroupsp) (int gidsetsize,
371                                            __kernel_gid32_t * grouplist);
372 #endif /* AFS_SPARC64_LINUX26_ENV */
373 asmlinkage long
374 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
375 {
376     long code;
377     cred_t *cr = crref();
378     afs_uint32 junk;
379     int old_pag;
380     
381     old_pag = PagInCred(cr);
382     crfree(cr);
383     
384     code = (*sys32_setgroupsp) (gidsetsize, grouplist);
385     if (code) {
386         return code;
387     }
388     
389     cr = crref();
390     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
391         /* re-install old pag if there's room. */
392         code = __setpag(&cr, old_pag, &junk, 0);
393     }
394     crfree(cr);
395     
396     /* Linux syscall ABI returns errno as negative */
397     return (-code);
398 }
399
400 /* Intercept the uid32 system call as used by 32bit programs. */
401 #ifdef AFS_AMD64_LINUX20_ENV
402 extern asmlinkage long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
403 #endif /* AFS_AMD64_LINUX20_ENV */
404 #ifdef AFS_SPARC64_LINUX26_ENV
405 extern asmlinkage int (*sys32_setgroups32p) (int gidsetsize,
406                                              __kernel_gid32_t * grouplist);
407 #endif /* AFS_SPARC64_LINUX26_ENV */
408 asmlinkage long
409 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
410 {
411     long code;
412     cred_t *cr = crref();
413     afs_uint32 junk;
414     int old_pag;
415
416     old_pag = PagInCred(cr);
417     crfree(cr);
418
419     code = (*sys32_setgroups32p) (gidsetsize, grouplist);
420     if (code) {
421         return code;
422     }
423
424     cr = crref();
425     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
426         /* re-install old pag if there's room. */
427         code = __setpag(&cr, old_pag, &junk, 0);
428     }
429     crfree(cr);
430
431     /* Linux syscall ABI returns errno as negative */
432     return (-code);
433 }
434 #endif
435
436
437 #ifdef LINUX_KEYRING_SUPPORT
438 static void afs_pag_describe(const struct key *key, struct seq_file *m)
439 {
440     seq_puts(m, key->description);
441
442     seq_printf(m, ": %u", key->datalen);
443 }
444
445 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
446 {
447     int code;
448     afs_uint32 *userpag, pag = NOPAG;
449
450     if (key->uid != 0 || key->gid != 0)
451         return -EPERM;
452
453     code = -EINVAL;
454     get_group_info(current_group_info());
455
456     if (datalen != sizeof(afs_uint32) || !data)
457         goto error;
458
459     /* ensure key being set matches current pag */
460     pag = afs_linux_pag_from_groups(current_group_info());
461
462     if (pag == NOPAG)
463         goto error;
464
465     userpag = (afs_uint32 *) data;
466     if (*userpag != pag)
467         goto error;
468
469     key->payload.value = (unsigned long) *userpag;
470     key->datalen = sizeof(afs_uint32);
471     code = 0;
472
473 error:
474     put_group_info(current_group_info());
475     return code;
476 }
477
478 static int afs_pag_match(const struct key *key, const void *description)
479 {
480         return strcmp(key->description, description) == 0;
481 }
482
483 static void afs_pag_destroy(struct key *key)
484 {
485     afs_uint32 pag = key->payload.value;
486     int locked = ISAFS_GLOCK();
487
488     if (!locked)
489         AFS_GLOCK();
490
491     afs_MarkUserExpired(pag);
492
493     if (!locked)
494         AFS_GUNLOCK();
495 }
496
497 struct key_type key_type_afs_pag =
498 {
499     .name        = "afs_pag",
500     .describe    = afs_pag_describe,
501     .instantiate = afs_pag_instantiate,
502     .match       = afs_pag_match,
503     .destroy     = afs_pag_destroy,
504 };
505
506 #ifdef EXPORTED_TASKLIST_LOCK
507 extern rwlock_t tasklist_lock __attribute__((weak));
508 #endif
509
510 void osi_keyring_init(void)
511 {
512 #if !defined(EXPORTED_KEY_TYPE_KEYRING)
513     struct task_struct *p;
514
515     /* If we can't lock the tasklist, either with its explicit lock,
516      * or by using the RCU lock, then we can't safely work out the 
517      * type of a keyring. So, we have to rely on the weak reference. 
518      * If that's not available, then keyring based PAGs won't work.
519      */
520     
521 #if defined(EXPORTED_TASKLIST_LOCK) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
522     if (__key_type_keyring == NULL) {
523 # ifdef EXPORTED_TASKLIST_LOCK
524         if (&tasklist_lock)
525             read_lock(&tasklist_lock);
526 # endif
527 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
528 #  ifdef EXPORTED_TASKLIST_LOCK
529         else
530 #  endif
531             rcu_read_lock();
532 # endif
533 #if defined(HAVE_LINUX_FIND_TASK_BY_PID)
534         p = find_task_by_pid(1);
535 #else
536         p = find_task_by_vpid(1);
537 #endif
538         if (p && task_user(p)->session_keyring)
539             __key_type_keyring = task_user(p)->session_keyring->type;
540 # ifdef EXPORTED_TASKLIST_LOCK
541         if (&tasklist_lock)
542             read_unlock(&tasklist_lock);
543 # endif
544 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
545 #  ifdef EXPORTED_TASKLIST_LOCK
546         else
547 #  endif
548             rcu_read_unlock();
549 # endif
550     }
551 #endif
552 #endif
553
554     register_key_type(&key_type_afs_pag);
555 }
556
557 void osi_keyring_shutdown(void)
558 {
559     unregister_key_type(&key_type_afs_pag);
560 }
561
562 afs_int32
563 osi_get_keyring_pag(afs_ucred_t *cred)
564 {
565     struct key *key;
566     afs_uint32 newpag;
567     afs_int32 keyring_pag = NOPAG;
568
569     if (afs_cr_rgid(cred) != NFSXLATOR_CRED) {
570         key = afs_linux_search_keyring(cred, &key_type_afs_pag);
571
572         if (!IS_ERR(key)) {
573             if (key_validate(key) == 0 && key->uid == 0) {      /* also verify in the session keyring? */
574                 keyring_pag = key->payload.value;
575                 /* Only set PAG in groups if needed,
576                  * and the creds are from the current process */
577                 if (afs_linux_cred_is_current(cred) &&
578                     ((keyring_pag >> 24) & 0xff) == 'A' &&
579                     keyring_pag != afs_linux_pag_from_groups(current_group_info())) {
580                         __setpag(&cred, keyring_pag, &newpag, 0);
581                 }
582             }
583             key_put(key);
584         }
585     }
586     return keyring_pag;
587 }
588
589 #else
590 void osi_keyring_init(void)
591 {
592         return;
593 }
594
595 void osi_keyring_shutdown(void)
596 {
597         return;
598 }
599 #endif