Coverage Report

Created: 2023-06-07 06:47

/src/sudo/lib/util/getgrouplist.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2010, 2011, 2013-2021
5
 *  Todd C. Miller <Todd.Miller@sudo.ws>
6
 *
7
 * Permission to use, copy, modify, and distribute this software for any
8
 * purpose with or without fee is hereby granted, provided that the above
9
 * copyright notice and this permission notice appear in all copies.
10
 *
11
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
 */
19
20
/*
21
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
22
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
23
 */
24
25
#include <config.h>
26
27
#include <stdlib.h>
28
#include <string.h>
29
#include <grp.h>
30
#include <limits.h>
31
#include <unistd.h>
32
#ifdef HAVE_NSS_SEARCH
33
# include <errno.h>
34
# include <limits.h>
35
# include <nsswitch.h>
36
# ifdef HAVE_NSS_DBDEFS_H
37
#  include <nss_dbdefs.h>
38
# else
39
#  include "compat/nss_dbdefs.h"
40
# endif
41
#endif
42
43
#include "sudo_compat.h"
44
#include "sudo_debug.h"
45
#include "sudo_util.h"
46
47
#ifndef HAVE_GETGROUPLIST
48
int
49
sudo_getgrouplist(const char *name, GETGROUPS_T basegid, GETGROUPS_T *groups,
50
    int *ngroupsp)
51
{
52
    return sudo_getgrouplist2(name, basegid, &groups, ngroupsp);
53
}
54
#endif /* HAVE_GETGROUPLIST */
55
56
#if defined(HAVE_GETGROUPLIST)
57
58
#if defined(HAVE_GETGROUPLIST_2) && !HAVE_DECL_GETGROUPLIST_2
59
int getgrouplist_2(const char *name, GETGROUPS_T basegid, GETGROUPS_T **groups);
60
#endif /* HAVE_GETGROUPLIST_2 && !HAVE_DECL_GETGROUPLIST_2 */
61
62
/*
63
 * Extended getgrouplist(3) using getgrouplist(3) and getgrouplist_2(3)
64
 */
65
int
66
sudo_getgrouplist2_v1(const char *name, GETGROUPS_T basegid,
67
    GETGROUPS_T **groupsp, int *ngroupsp)
68
28.6k
{
69
#ifdef __APPLE__
70
    int *groups = (int *)*groupsp;
71
#else
72
28.6k
    GETGROUPS_T *groups = *groupsp;
73
28.6k
#endif
74
28.6k
    int ngroups;
75
28.6k
#ifndef HAVE_GETGROUPLIST_2
76
28.6k
    int grpsize, tries;
77
28.6k
#endif
78
28.6k
    debug_decl(sudo_getgrouplist2, SUDO_DEBUG_UTIL);
79
80
    /* For static group vector, just use getgrouplist(3). */
81
28.6k
    if (groups != NULL)
82
56
  debug_return_int(getgrouplist(name, basegid, groups, ngroupsp));
83
84
#ifdef HAVE_GETGROUPLIST_2
85
    if ((ngroups = getgrouplist_2(name, basegid, groupsp)) == -1)
86
  debug_return_int(-1);
87
    *ngroupsp = ngroups;
88
    debug_return_int(0);
89
#else
90
28.5k
    grpsize = (int)sysconf(_SC_NGROUPS_MAX);
91
28.5k
    if (grpsize < 0)
92
0
  grpsize = NGROUPS_MAX;
93
28.5k
    grpsize++;  /* include space for the primary gid */
94
    /*
95
     * It is possible to belong to more groups in the group database
96
     * than NGROUPS_MAX.
97
     */
98
28.5k
    for (tries = 0; tries < 10; tries++) {
99
28.5k
  free(groups);
100
28.5k
  groups = reallocarray(NULL, grpsize, sizeof(*groups));
101
28.5k
  if (groups == NULL)
102
0
      debug_return_int(-1);
103
28.5k
  ngroups = grpsize;
104
28.5k
  if (getgrouplist(name, basegid, groups, &ngroups) != -1) {
105
28.5k
      *groupsp = groups;
106
28.5k
      *ngroupsp = ngroups;
107
28.5k
      debug_return_int(0);
108
28.5k
  }
109
0
  if (ngroups == grpsize) {
110
      /* Failed for some reason other than ngroups too small. */
111
0
      break;
112
0
  }
113
  /* getgrouplist(3) set ngroups to the required length, use it. */
114
0
  grpsize = ngroups;
115
0
    }
116
0
    free(groups);
117
0
    debug_return_int(-1);
118
0
#endif /* HAVE_GETGROUPLIST_2 */
119
0
}
120
121
#elif defined(HAVE_GETGRSET)
122
123
/*
124
 * Extended getgrouplist(3) using AIX getgrset(3)
125
 */
126
int
127
sudo_getgrouplist2_v1(const char *name, GETGROUPS_T basegid,
128
    GETGROUPS_T **groupsp, int *ngroupsp)
129
{
130
    GETGROUPS_T *groups = *groupsp;
131
    char *cp, *last, *grset = NULL;
132
    const char *errstr;
133
    int ngroups = 1;
134
    int grpsize = *ngroupsp;
135
    int ret = -1;
136
    gid_t gid;
137
    debug_decl(sudo_getgrouplist2, SUDO_DEBUG_UTIL);
138
139
#ifdef HAVE_SETAUTHDB
140
    aix_setauthdb((char *) name, NULL);
141
#endif
142
    if ((grset = getgrset(name)) == NULL)
143
  goto done;
144
145
    if (groups == NULL) {
146
  /* Dynamically-sized group vector, count groups and alloc. */
147
  grpsize = 1;  /* reserve one for basegid */
148
  if (*grset != '\0') {
149
      grpsize++;  /* at least one supplementary group */
150
      for (cp = grset; *cp != '\0'; cp++) {
151
    if (*cp == ',')
152
        grpsize++;
153
      }
154
  }
155
  groups = reallocarray(NULL, grpsize, sizeof(*groups));
156
  if (groups == NULL)
157
      debug_return_int(-1);
158
    } else {
159
  /* Static group vector. */
160
  if (grpsize < 1)
161
      debug_return_int(-1);
162
    }
163
164
    /* We support BSD semantics where the first element is the base gid */
165
    groups[0] = basegid;
166
167
    for (cp = strtok_r(grset, ",", &last); cp != NULL; cp = strtok_r(NULL, ",", &last)) {
168
  gid = sudo_strtoid(cp, &errstr);
169
  if (errstr == NULL && gid != basegid) {
170
      if (ngroups == grpsize)
171
    goto done;
172
      groups[ngroups++] = gid;
173
  }
174
    }
175
    ret = 0;
176
177
done:
178
    free(grset);
179
#ifdef HAVE_SETAUTHDB
180
    aix_restoreauthdb();
181
#endif
182
    *groupsp = groups;
183
    *ngroupsp = ngroups;
184
185
    debug_return_int(ret);
186
}
187
188
#elif defined(HAVE_NSS_SEARCH)
189
190
#ifndef ALIGNBYTES
191
# define ALIGNBYTES (sizeof(long) - 1L)
192
#endif
193
#ifndef ALIGN
194
# define ALIGN(p) (((unsigned long)(p) + ALIGNBYTES) & ~ALIGNBYTES)
195
#endif
196
197
#if defined(HAVE__NSS_INITF_GROUP) || defined(HAVE___NSS_INITF_GROUP)
198
extern void _nss_initf_group(nss_db_params_t *params);
199
#else
200
static void
201
_nss_initf_group(nss_db_params_t *params)
202
{
203
    params->name = NSS_DBNAM_GROUP;
204
    params->default_config = NSS_DEFCONF_GROUP;
205
}
206
#endif
207
208
/*
209
 * Convert a groups file string (instr) to a struct group (ent) using
210
 * buf for storage.  
211
 */
212
static int
213
str2grp(const char *instr, int inlen, void *ent, char *buf, int buflen)
214
{
215
    struct group *grp = ent;
216
    char *cp, *fieldsep = buf;
217
    char **gr_mem, **gr_end;
218
    const char *errstr;
219
    int yp = 0;
220
    id_t id;
221
    debug_decl(str2grp, SUDO_DEBUG_UTIL);
222
223
    /* Must at least have space to copy instr -> buf. */
224
    if (inlen >= buflen)
225
  debug_return_int(NSS_STR_PARSE_ERANGE);
226
    
227
    /* Paranoia: buf and instr should be distinct. */
228
    if (buf != instr) {
229
  memmove(buf, instr, inlen);
230
  buf[inlen] = '\0';
231
    }
232
233
    if ((fieldsep = strchr(cp = fieldsep, ':')) == NULL)
234
  debug_return_int(NSS_STR_PARSE_PARSE);
235
    *fieldsep++ = '\0';
236
    grp->gr_name = cp;
237
238
    /* Check for YP inclusion/exclusion entries. */
239
    if (*cp == '+' || *cp == '-') {
240
  /* Only the name is required for YP inclusion/exclusion entries. */
241
  grp->gr_passwd = (char *)"";
242
  grp->gr_gid = 0;
243
  grp->gr_mem = NULL;
244
  yp = 1;
245
    }
246
247
    if ((fieldsep = strchr(cp = fieldsep, ':')) == NULL)
248
  debug_return_int(yp ? NSS_STR_PARSE_SUCCESS : NSS_STR_PARSE_PARSE);
249
    *fieldsep++ = '\0';
250
    grp->gr_passwd = cp;
251
252
    if ((fieldsep = strchr(cp = fieldsep, ':')) == NULL)
253
  debug_return_int(yp ? NSS_STR_PARSE_SUCCESS : NSS_STR_PARSE_PARSE);
254
    *fieldsep++ = '\0';
255
    id = sudo_strtoid(cp, &errstr);
256
    if (errstr != NULL) {
257
  /*
258
   * A range error is always a fatal error, but ignore garbage
259
   * at the end of YP entries since it has no meaning.
260
   */
261
  if (errno == ERANGE)
262
      debug_return_int(NSS_STR_PARSE_ERANGE);
263
  debug_return_int(yp ? NSS_STR_PARSE_SUCCESS : NSS_STR_PARSE_PARSE);
264
    }
265
#ifdef GID_NOBODY
266
    /* Negative gids get mapped to nobody on Solaris. */
267
    if (*cp == '-' && id != 0)
268
  grp->gr_gid = GID_NOBODY;
269
    else
270
#endif
271
  grp->gr_gid = (gid_t)id;
272
273
    /* Store group members, taking care to use proper alignment. */
274
    grp->gr_mem = NULL;
275
    if (*fieldsep != '\0') {
276
  grp->gr_mem = gr_mem = (char **)ALIGN(buf + inlen + 1);
277
  gr_end = (char **)((unsigned long)(buf + buflen) & ~ALIGNBYTES) - 1;
278
  for (;;) {
279
      if (gr_mem >= gr_end)
280
    debug_return_int(NSS_STR_PARSE_ERANGE); /* out of space! */
281
      *gr_mem++ = cp;
282
      if (fieldsep == NULL)
283
    break;
284
      if ((fieldsep = strchr(cp = fieldsep, ',')) != NULL)
285
    *fieldsep++ = '\0';
286
  }
287
  *gr_mem = NULL;
288
    }
289
    debug_return_int(NSS_STR_PARSE_SUCCESS);
290
}
291
292
static nss_status_t
293
process_cstr(const char *instr, int inlen, struct nss_groupsbymem *gbm,
294
    int dynamic)
295
{
296
    const char *user = gbm->username;
297
    nss_status_t ret = NSS_NOTFOUND;
298
    nss_XbyY_buf_t *buf;
299
    struct group *grp;
300
    char **gr_mem;
301
    int error, i;
302
    debug_decl(process_cstr, SUDO_DEBUG_UTIL);
303
304
    sudo_debug_printf(SUDO_DEBUG_INFO, "%s: parsing %.*s", __func__,
305
  inlen, instr);
306
307
    /* Hack to let us check whether the query was handled by nscd or us. */
308
    if (gbm->force_slow_way != 0)
309
  gbm->force_slow_way = 2;
310
311
    buf = _nss_XbyY_buf_alloc(sizeof(struct group), NSS_BUFLEN_GROUP);
312
    if (buf == NULL)
313
  debug_return_int(NSS_UNAVAIL);
314
315
    /* Parse groups file string -> struct group. */
316
    grp = buf->result;
317
    error = (*gbm->str2ent)(instr, inlen, grp, buf->buffer, buf->buflen);
318
    if (error != NSS_STR_PARSE_SUCCESS || grp->gr_mem == NULL)
319
  goto done;
320
321
    for (gr_mem = grp->gr_mem; *gr_mem != NULL; gr_mem++) {
322
  if (strcmp(*gr_mem, user) == 0) {
323
      const int numgids = MIN(gbm->numgids, gbm->maxgids);
324
325
      /* Append to gid_array unless gr_gid is a dupe. */
326
      for (i = 0; i < numgids; i++) {
327
    if (gbm->gid_array[i] == grp->gr_gid)
328
        goto done;      /* already present */
329
      }
330
      if (i == gbm->maxgids && dynamic) {
331
    GETGROUPS_T *tmp = reallocarray(gbm->gid_array, gbm->maxgids,
332
        2 * sizeof(GETGROUPS_T));
333
    if (tmp == NULL) {
334
        /* Out of memory, just return what we have. */
335
        dynamic = 0;
336
    } else {
337
        gbm->gid_array = tmp;
338
        gbm->maxgids <<= 1;
339
    }
340
      }
341
      /* Store gid if there is space. */
342
      if (i < gbm->maxgids)
343
    gbm->gid_array[i] = grp->gr_gid;
344
      /* Always increment numgids so we can detect when out of space. */
345
      gbm->numgids++;
346
      goto done;
347
  }
348
    }
349
done:
350
    _nss_XbyY_buf_free(buf);
351
    debug_return_int(ret);
352
}
353
354
static nss_status_t
355
process_cstr_static(const char *instr, int inlen, struct nss_groupsbymem *gbm)
356
{
357
    return process_cstr(instr, inlen, gbm, 0);
358
}
359
360
static nss_status_t
361
process_cstr_dynamic(const char *instr, int inlen, struct nss_groupsbymem *gbm)
362
{
363
    return process_cstr(instr, inlen, gbm, 1);
364
}
365
366
/*
367
 * Extended getgrouplist(3) using nss_search(3)
368
 */
369
int
370
sudo_getgrouplist2_v1(const char *name, GETGROUPS_T basegid,
371
    GETGROUPS_T **groupsp, int *ngroupsp)
372
{
373
    struct nss_groupsbymem gbm;
374
    static DEFINE_NSS_DB_ROOT(db_root);
375
    debug_decl(sudo_getgrouplist2, SUDO_DEBUG_UTIL);
376
377
    memset(&gbm, 0, sizeof(gbm));
378
    gbm.username = name;
379
    gbm.gid_array = *groupsp;
380
    gbm.maxgids = *ngroupsp;
381
    gbm.numgids = 1; /* for basegid */
382
    gbm.force_slow_way = 1;
383
    gbm.str2ent = str2grp;
384
385
    if (gbm.gid_array == NULL) {
386
  /* Dynamically-sized group vector. */
387
  gbm.maxgids = (int)sysconf(_SC_NGROUPS_MAX);
388
  if (gbm.maxgids < 0)
389
      gbm.maxgids = NGROUPS_MAX;
390
  gbm.gid_array = reallocarray(NULL, gbm.maxgids, 4 * sizeof(GETGROUPS_T));
391
  if (gbm.gid_array == NULL)
392
      debug_return_int(-1);
393
  gbm.maxgids <<= 2;
394
  gbm.process_cstr = process_cstr_dynamic;
395
    } else {
396
  /* Static group vector. */
397
  if (gbm.maxgids <= 0)
398
      debug_return_int(-1);
399
  gbm.process_cstr = process_cstr_static;
400
    }
401
402
    /* We support BSD semantics where the first element is the base gid */
403
    gbm.gid_array[0] = basegid;
404
405
    /*
406
     * Can't use nss_search return value since it may return NSS_UNAVAIL
407
     * when no nsswitch.conf entry (e.g. compat mode).
408
     */
409
    for (;;) {
410
  GETGROUPS_T *tmp;
411
412
  (void)nss_search(&db_root, _nss_initf_group, NSS_DBOP_GROUP_BYMEMBER,
413
      &gbm);
414
415
  /*
416
   * If this was a statically-sized group vector or nscd was not used
417
   * we are done.
418
   */
419
  if (gbm.process_cstr != process_cstr_dynamic || gbm.force_slow_way == 2)
420
      break;
421
422
  /*
423
   * If gid_array is full and the query was handled by nscd, there
424
   * may be more data, so double gid_array and try again.
425
   */
426
  if (gbm.numgids != gbm.maxgids)
427
      break;
428
429
  tmp = reallocarray(gbm.gid_array, gbm.maxgids, 2 * sizeof(GETGROUPS_T));
430
  if (tmp == NULL) {
431
      free(gbm.gid_array);
432
      debug_return_int(-1);
433
  }
434
  gbm.gid_array = tmp;
435
  gbm.maxgids <<= 1;
436
    }
437
438
    /* Note: we can only detect a too-small group list if nscd is not used. */
439
    *groupsp = gbm.gid_array;
440
    if (gbm.numgids <= gbm.maxgids) {
441
        *ngroupsp = gbm.numgids;
442
  debug_return_int(0);
443
    }
444
    *ngroupsp = gbm.maxgids;
445
    debug_return_int(-1);
446
}
447
448
#else /* !HAVE_GETGROUPLIST && !HAVE_GETGRSET && !HAVE__GETGROUPSBYMEMBER */
449
450
/*
451
 * Extended getgrouplist(3) using getgrent(3)
452
 */
453
int
454
sudo_getgrouplist2_v1(const char *name, GETGROUPS_T basegid,
455
    GETGROUPS_T **groupsp, int *ngroupsp)
456
{
457
    GETGROUPS_T *groups = *groupsp;
458
    int grpsize = *ngroupsp;
459
    int i, ngroups = 1;
460
    int ret = -1;
461
    struct group *grp;
462
    debug_decl(sudo_getgrouplist2, SUDO_DEBUG_UTIL);
463
464
    if (groups == NULL) {
465
  /* Dynamically-sized group vector. */
466
  grpsize = (int)sysconf(_SC_NGROUPS_MAX);
467
  if (grpsize < 0)
468
      grpsize = NGROUPS_MAX;
469
  groups = reallocarray(NULL, grpsize, 4 * sizeof(*groups));
470
  if (groups == NULL)
471
      debug_return_int(-1);
472
  grpsize <<= 2;
473
    } else {
474
  /* Static group vector. */
475
  if (grpsize < 1)
476
      debug_return_int(-1);
477
    }
478
479
    /* We support BSD semantics where the first element is the base gid */
480
    groups[0] = basegid;
481
482
    setgrent();
483
    while ((grp = getgrent()) != NULL) {
484
  if (grp->gr_gid == basegid || grp->gr_mem == NULL)
485
      continue;
486
487
  for (i = 0; grp->gr_mem[i] != NULL; i++) {
488
      if (strcmp(name, grp->gr_mem[i]) == 0)
489
    break;
490
  }
491
  if (grp->gr_mem[i] == NULL)
492
      continue; /* user not found */
493
494
  /* Only add if it is not the same as an existing gid */
495
  for (i = 0; i < ngroups; i++) {
496
      if (grp->gr_gid == groups[i])
497
    break;
498
  }
499
  if (i == ngroups) {
500
      if (ngroups == grpsize) {
501
    GETGROUPS_T *tmp;
502
503
    if (*groupsp != NULL) {
504
        /* Static group vector. */
505
        goto done;
506
    }
507
    tmp = reallocarray(groups, grpsize, 2 * sizeof(*groups));
508
    if (tmp == NULL) {
509
        free(groups);
510
        groups = NULL;
511
        ngroups = 0;
512
        goto done;
513
    }
514
    groups = tmp;
515
    grpsize <<= 1;
516
      }
517
      groups[ngroups++] = grp->gr_gid;
518
  }
519
    }
520
    ret = 0;
521
522
done:
523
    endgrent();
524
    *groupsp = groups;
525
    *ngroupsp = ngroups;
526
527
    debug_return_int(ret);
528
}
529
#endif /* !HAVE_GETGROUPLIST && !HAVE_GETGRSET && !HAVE__GETGROUPSBYMEMBER */