]>
git.proxmox.com Git - mirror_spl-debian.git/blob - module/splat/splat-cred.c
db36ece980f9594929ea3f4df671ae26ef61d30e
1 /*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting LAyer Tests (SPLAT) Credential Tests.
25 \*****************************************************************************/
27 #include "splat-internal.h"
29 #define SPLAT_CRED_NAME "cred"
30 #define SPLAT_CRED_DESC "Kernel Cred Tests"
32 #define SPLAT_CRED_TEST1_ID 0x0e01
33 #define SPLAT_CRED_TEST1_NAME "cred"
34 #define SPLAT_CRED_TEST1_DESC "Task Credential Test"
36 #define SPLAT_CRED_TEST2_ID 0x0e02
37 #define SPLAT_CRED_TEST2_NAME "kcred"
38 #define SPLAT_CRED_TEST2_DESC "Kernel Credential Test"
40 #define SPLAT_CRED_TEST3_ID 0x0e03
41 #define SPLAT_CRED_TEST3_NAME "groupmember"
42 #define SPLAT_CRED_TEST3_DESC "Group Member Test"
44 #define GROUP_STR_SIZE 128
45 #define GROUP_STR_REDZONE 16
48 splat_cred_test1(struct file
*file
, void *arg
)
50 char str
[GROUP_STR_SIZE
];
51 uid_t uid
, ruid
, suid
;
52 gid_t gid
, rgid
, sgid
, *groups
;
53 int ngroups
, i
, count
= 0;
55 uid
= crgetuid(CRED());
56 ruid
= crgetruid(CRED());
57 suid
= crgetsuid(CRED());
59 gid
= crgetgid(CRED());
60 rgid
= crgetrgid(CRED());
61 sgid
= crgetsgid(CRED());
64 ngroups
= crgetngroups(CRED());
65 groups
= crgetgroups(CRED());
67 memset(str
, 0, GROUP_STR_SIZE
);
68 for (i
= 0; i
< ngroups
; i
++) {
69 count
+= sprintf(str
+ count
, "%d ", groups
[i
]);
71 if (count
> (GROUP_STR_SIZE
- GROUP_STR_REDZONE
)) {
72 splat_vprint(file
, SPLAT_CRED_TEST1_NAME
,
73 "Failed too many group entries for temp "
74 "buffer: %d, %s\n", ngroups
, str
);
81 splat_vprint(file
, SPLAT_CRED_TEST1_NAME
,
82 "uid: %d ruid: %d suid: %d "
83 "gid: %d rgid: %d sgid: %d\n",
84 uid
, ruid
, suid
, gid
, rgid
, sgid
);
85 splat_vprint(file
, SPLAT_CRED_TEST1_NAME
,
86 "ngroups: %d groups: %s\n", ngroups
, str
);
88 if (uid
|| ruid
|| suid
|| gid
|| rgid
|| sgid
) {
89 splat_vprint(file
, SPLAT_CRED_TEST1_NAME
,
90 "Failed expected all uids+gids to be %d\n", 0);
94 if (ngroups
> NGROUPS_MAX
) {
95 splat_vprint(file
, SPLAT_CRED_TEST1_NAME
,
96 "Failed ngroups must not exceed NGROUPS_MAX: "
97 "%d > %d\n", ngroups
, NGROUPS_MAX
);
101 splat_vprint(file
, SPLAT_CRED_TEST1_NAME
,
102 "Success sane CRED(): %d\n", 0);
105 } /* splat_cred_test1() */
108 splat_cred_test2(struct file
*file
, void *arg
)
110 char str
[GROUP_STR_SIZE
];
111 uid_t uid
, ruid
, suid
;
112 gid_t gid
, rgid
, sgid
, *groups
;
113 int ngroups
, i
, count
= 0;
115 uid
= crgetuid(kcred
);
116 ruid
= crgetruid(kcred
);
117 suid
= crgetsuid(kcred
);
119 gid
= crgetgid(kcred
);
120 rgid
= crgetrgid(kcred
);
121 sgid
= crgetsgid(kcred
);
124 ngroups
= crgetngroups(kcred
);
125 groups
= crgetgroups(kcred
);
127 memset(str
, 0, GROUP_STR_SIZE
);
128 for (i
= 0; i
< ngroups
; i
++) {
129 count
+= sprintf(str
+ count
, "%d ", groups
[i
]);
131 if (count
> (GROUP_STR_SIZE
- GROUP_STR_REDZONE
)) {
132 splat_vprint(file
, SPLAT_CRED_TEST2_NAME
,
133 "Failed too many group entries for temp "
134 "buffer: %d, %s\n", ngroups
, str
);
141 splat_vprint(file
, SPLAT_CRED_TEST2_NAME
,
142 "uid: %d ruid: %d suid: %d "
143 "gid: %d rgid: %d sgid: %d\n",
144 uid
, ruid
, suid
, gid
, rgid
, sgid
);
145 splat_vprint(file
, SPLAT_CRED_TEST2_NAME
,
146 "ngroups: %d groups: %s\n", ngroups
, str
);
148 if (uid
|| ruid
|| suid
|| gid
|| rgid
|| sgid
) {
149 splat_vprint(file
, SPLAT_CRED_TEST2_NAME
,
150 "Failed expected all uids+gids to be %d\n", 0);
154 if (ngroups
> NGROUPS_MAX
) {
155 splat_vprint(file
, SPLAT_CRED_TEST2_NAME
,
156 "Failed ngroups must not exceed NGROUPS_MAX: "
157 "%d > %d\n", ngroups
, NGROUPS_MAX
);
161 splat_vprint(file
, SPLAT_CRED_TEST2_NAME
,
162 "Success sane kcred: %d\n", 0);
165 } /* splat_cred_test2() */
168 * On most/all systems it can be expected that a task with root
169 * permissions also is a member of the root group, Since the
170 * test suite is always run as root we check first that CRED() is
171 * a member of the root group, and secondly that it is not a member
172 * of our fake group. This test will break is someone happens to
173 * create group number NGROUPS_MAX-1 and then added root to it.
176 splat_cred_test3(struct file
*file
, void *arg
)
178 gid_t root_gid
, fake_gid
;
182 fake_gid
= NGROUPS_MAX
-1;
184 rc
= groupmember(root_gid
, CRED());
186 splat_vprint(file
, SPLAT_CRED_TEST3_NAME
,
187 "Failed root git %d expected to be member "
188 "of CRED() groups: %d\n", root_gid
, rc
);
192 rc
= groupmember(fake_gid
, CRED());
194 splat_vprint(file
, SPLAT_CRED_TEST3_NAME
,
195 "Failed fake git %d expected not to be member "
196 "of CRED() groups: %d\n", fake_gid
, rc
);
200 splat_vprint(file
, SPLAT_CRED_TEST3_NAME
, "Success root gid "
201 "is a member of the expected groups: %d\n", rc
);
204 } /* splat_cred_test3() */
207 splat_cred_init(void)
209 splat_subsystem_t
*sub
;
211 sub
= kmalloc(sizeof(*sub
), GFP_KERNEL
);
215 memset(sub
, 0, sizeof(*sub
));
216 strncpy(sub
->desc
.name
, SPLAT_CRED_NAME
, SPLAT_NAME_SIZE
);
217 strncpy(sub
->desc
.desc
, SPLAT_CRED_DESC
, SPLAT_DESC_SIZE
);
218 INIT_LIST_HEAD(&sub
->subsystem_list
);
219 INIT_LIST_HEAD(&sub
->test_list
);
220 spin_lock_init(&sub
->test_lock
);
221 sub
->desc
.id
= SPLAT_SUBSYSTEM_CRED
;
223 SPLAT_TEST_INIT(sub
, SPLAT_CRED_TEST1_NAME
, SPLAT_CRED_TEST1_DESC
,
224 SPLAT_CRED_TEST1_ID
, splat_cred_test1
);
225 SPLAT_TEST_INIT(sub
, SPLAT_CRED_TEST2_NAME
, SPLAT_CRED_TEST2_DESC
,
226 SPLAT_CRED_TEST2_ID
, splat_cred_test2
);
227 SPLAT_TEST_INIT(sub
, SPLAT_CRED_TEST3_NAME
, SPLAT_CRED_TEST3_DESC
,
228 SPLAT_CRED_TEST3_ID
, splat_cred_test3
);
231 } /* splat_cred_init() */
234 splat_cred_fini(splat_subsystem_t
*sub
)
238 SPLAT_TEST_FINI(sub
, SPLAT_CRED_TEST3_ID
);
239 SPLAT_TEST_FINI(sub
, SPLAT_CRED_TEST2_ID
);
240 SPLAT_TEST_FINI(sub
, SPLAT_CRED_TEST1_ID
);
243 } /* splat_cred_fini() */
248 return SPLAT_SUBSYSTEM_CRED
;
249 } /* splat_cred_id() */