~ubuntu-branches/ubuntu/wily/spl-linux/wily-proposed

« back to all changes in this revision

Viewing changes to module/splat/splat-cred.c

  • Committer: Package Import Robot
  • Author(s): Liang Guo
  • Date: 2014-07-31 15:16:53 UTC
  • Revision ID: package-import@ubuntu.com-20140731151653-tgao12alohj26jcs
Tags: upstream-0.6.3+git20140731
ImportĀ upstreamĀ versionĀ 0.6.3+git20140731

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
\*****************************************************************************/
26
26
 
27
27
#include <sys/cred.h>
 
28
#include <sys/random.h>
28
29
#include "splat-internal.h"
29
30
 
30
31
#define SPLAT_CRED_NAME                 "cred"
166
167
} /* splat_cred_test2() */
167
168
 
168
169
/*
169
 
 * On most/all systems it can be expected that a task with root
170
 
 * permissions also is a member of the root group,  Since the
171
 
 * test suite is always run as root we check first that CRED() is
172
 
 * a member of the root group, and secondly that it is not a member
173
 
 * of our fake group.  This test will break is someone happens to
174
 
 * create group number NGROUPS_MAX-1 and then added root to it.
 
170
 * Verify the groupmember() works correctly by constructing an interesting
 
171
 * CRED() and checking that the expected gids are part of it.
175
172
 */
176
173
static int
177
174
splat_cred_test3(struct file *file, void *arg)
178
175
{
179
 
        gid_t root_gid, fake_gid;
180
 
        int rc;
181
 
 
182
 
        root_gid = 0;
183
 
        fake_gid = NGROUPS_MAX-1;
184
 
 
185
 
        rc = groupmember(root_gid, CRED());
 
176
        gid_t known_gid, missing_gid, tmp_gid;
 
177
        unsigned char rnd;
 
178
        struct group_info *gi;
 
179
        int i, rc;
 
180
 
 
181
        get_random_bytes((void *)&rnd, 1);
 
182
        known_gid = (rnd > 0) ? rnd : 1;
 
183
        missing_gid = 0;
 
184
 
 
185
        /*
 
186
         * Create an interesting known set of gids for test purposes. The
 
187
         * gids are pseudo randomly selected are will be in the range of
 
188
         * 1:(NGROUPS_MAX-1).  Gid 0 is explicitly avoided so we can reliably
 
189
         * test for its absence in the test cases.
 
190
         */
 
191
        gi = groups_alloc(NGROUPS_SMALL);
 
192
        if (gi == NULL) {
 
193
                splat_vprint(file, SPLAT_CRED_TEST3_NAME, "Failed create "
 
194
                    "group_info for known gids: %d\n", -ENOMEM);
 
195
                rc = -ENOMEM;
 
196
                goto show_groups;
 
197
        }
 
198
 
 
199
        for (i = 0, tmp_gid = known_gid; i < NGROUPS_SMALL; i++) {
 
200
                splat_vprint(file, SPLAT_CRED_TEST3_NAME, "Adding gid %d "
 
201
                    "to current CRED() (%d/%d)\n", tmp_gid, i, gi->ngroups);
 
202
#ifdef HAVE_KUIDGID_T
 
203
                GROUP_AT(gi, i) = make_kgid(current_user_ns(), tmp_gid);
 
204
#else
 
205
                GROUP_AT(gi, i) = tmp_gid;
 
206
#endif /* HAVE_KUIDGID_T */
 
207
                tmp_gid = ((tmp_gid * 17) % (NGROUPS_MAX - 1)) + 1;
 
208
        }
 
209
 
 
210
        /* Set the new groups in the CRED() and release our reference. */
 
211
        rc = set_current_groups(gi);
 
212
        put_group_info(gi);
 
213
 
 
214
        if (rc) {
 
215
                splat_vprint(file, SPLAT_CRED_TEST3_NAME, "Failed to add "
 
216
                    "gid %d to current group: %d\n", known_gid, rc);
 
217
                goto show_groups;
 
218
        }
 
219
 
 
220
        /* Verify groupmember() finds the known_gid in the CRED() */
 
221
        rc = groupmember(known_gid, CRED());
186
222
        if (!rc) {
187
 
                splat_vprint(file, SPLAT_CRED_TEST3_NAME,
188
 
                             "Failed root git %d expected to be member "
189
 
                             "of CRED() groups: %d\n", root_gid, rc);
190
 
                return -EIDRM;
191
 
        }
192
 
 
193
 
        rc = groupmember(fake_gid, CRED());
194
 
        if (rc) {
195
 
                splat_vprint(file, SPLAT_CRED_TEST3_NAME,
196
 
                             "Failed fake git %d expected not to be member "
197
 
                             "of CRED() groups: %d\n", fake_gid, rc);
198
 
                return -EIDRM;
199
 
        }
200
 
 
201
 
        splat_vprint(file, SPLAT_CRED_TEST3_NAME, "Success root gid "
202
 
                     "is a member of the expected groups: %d\n", rc);
203
 
 
204
 
        return rc;
 
223
                splat_vprint(file, SPLAT_CRED_TEST3_NAME, "Failed to find "
 
224
                    "known gid %d in CRED()'s groups.\n", known_gid);
 
225
                rc = -EIDRM;
 
226
                goto show_groups;
 
227
        }
 
228
 
 
229
        /* Verify groupmember() does NOT finds the missing gid in the CRED() */
 
230
        rc = groupmember(missing_gid, CRED());
 
231
        if (rc) {
 
232
                splat_vprint(file, SPLAT_CRED_TEST3_NAME, "Failed missing "
 
233
                    "gid %d was found in CRED()'s groups.\n", missing_gid);
 
234
                rc = -EIDRM;
 
235
                goto show_groups;
 
236
        }
 
237
 
 
238
        splat_vprint(file, SPLAT_CRED_TEST3_NAME, "Success groupmember() "
 
239
            "correctly detects expected gids in CRED(): %d\n", rc);
 
240
 
 
241
show_groups:
 
242
        if (rc) {
 
243
                int i, grps = crgetngroups(CRED());
 
244
 
 
245
                splat_vprint(file, SPLAT_CRED_TEST3_NAME, "%d groups: ", grps);
 
246
                for (i = 0; i < grps; i++)
 
247
                        splat_print(file, "%d ", crgetgroups(CRED())[i]);
 
248
                splat_print(file, "%s", "\n");
 
249
        }
 
250
 
 
251
 
 
252
        return (rc);
205
253
} /* splat_cred_test3() */
206
254
 
207
255
splat_subsystem_t *