| 1 |
/**************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/**************************************************************************/ |
| 22 |
|
| 23 |
#include "__fc_builtin.h" |
| 24 |
#include "errno.h" |
| 25 |
#include "pwd.h" |
| 26 |
#include "string.h" |
| 27 |
|
| 28 |
__PUSH_FC_STDLIB |
| 29 |
|
| 30 |
char __fc_getpw_pw_name[64]; |
| 31 |
char __fc_getpw_pw_passwd[64]; |
| 32 |
char __fc_getpw_pw_gecos[64]; |
| 33 |
char __fc_getpw_pw_dir[64]; |
| 34 |
char __fc_getpw_pw_shell[64]; |
| 35 |
|
| 36 |
static int __fc_getpw_init; // used to initialize the __fc_getpw* strings |
| 37 |
|
| 38 |
#define COPY_FIELD(fieldname) \ |
| 39 |
len = strlen(__fc_pwd.fieldname); \ |
| 40 |
if (remaining <= len) { \ |
| 41 |
*result = NULL; \ |
| 42 |
return ERANGE; \ |
| 43 |
} \ |
| 44 |
strcpy(buf, __fc_pwd.fieldname); \ |
| 45 |
buf[len] = 0; \ |
| 46 |
pwd->fieldname = buf; \ |
| 47 |
buf += len + 1; \ |
| 48 |
remaining -= len |
| 49 |
|
| 50 |
// Common code between getpwman_r and getpwuid_r |
| 51 |
static int __fc_getpw_r(struct passwd *pwd, char *buf, |
| 52 |
size_t buflen, struct passwd **result) { |
| 53 |
// initialize global strings (only during first execution) |
| 54 |
if (!__fc_getpw_init) { |
| 55 |
__fc_getpw_init = 1; |
| 56 |
Frama_C_make_unknown(__fc_getpw_pw_name, 63); |
| 57 |
__fc_getpw_pw_name[63] = 0; |
| 58 |
Frama_C_make_unknown(__fc_getpw_pw_passwd, 63); |
| 59 |
__fc_getpw_pw_passwd[63] = 0; |
| 60 |
Frama_C_make_unknown(__fc_getpw_pw_gecos, 63); |
| 61 |
__fc_getpw_pw_gecos[63] = 0; |
| 62 |
Frama_C_make_unknown(__fc_getpw_pw_dir, 63); |
| 63 |
__fc_getpw_pw_dir[63] = 0; |
| 64 |
Frama_C_make_unknown(__fc_getpw_pw_shell, 63); |
| 65 |
__fc_getpw_pw_shell[63] = 0; |
| 66 |
} |
| 67 |
|
| 68 |
// simulate one of several possible errors, or "not found" |
| 69 |
if (Frama_C_interval(0, 1)) { |
| 70 |
// 'error or not found' case |
| 71 |
*result = NULL; |
| 72 |
if (Frama_C_interval(0, 1)) return EIO; |
| 73 |
if (Frama_C_interval(0, 1)) return EMFILE; |
| 74 |
if (Frama_C_interval(0, 1)) return ENFILE; |
| 75 |
// Note: POSIX only specifies these values, but several implementations |
| 76 |
// return others, including ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM. |
| 77 |
return 0; |
| 78 |
} else { |
| 79 |
// 'found without errors' case |
| 80 |
size_t len, remaining = buflen; |
| 81 |
// copy string fields to buf |
| 82 |
COPY_FIELD(pw_name); |
| 83 |
COPY_FIELD(pw_passwd); |
| 84 |
COPY_FIELD(pw_gecos); |
| 85 |
COPY_FIELD(pw_dir); |
| 86 |
COPY_FIELD(pw_shell); |
| 87 |
pwd->pw_uid = Frama_C_unsigned_int_interval(0, UINT_MAX); |
| 88 |
pwd->pw_gid = Frama_C_unsigned_int_interval(0, UINT_MAX); |
| 89 |
*result = pwd; |
| 90 |
return 0; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
int getpwnam_r(const char *name, struct passwd *pwd, |
| 95 |
char *buf, size_t buflen, struct passwd **result) { |
| 96 |
//@ assert valid_read_string(name); |
| 97 |
return __fc_getpw_r(pwd, buf, buflen, result); |
| 98 |
} |
| 99 |
|
| 100 |
int getpwuid_r(uid_t uid, struct passwd *pwd, |
| 101 |
char *buf, size_t buflen, struct passwd **result) { |
| 102 |
return __fc_getpw_r(pwd, buf, buflen, result); |
| 103 |
} |
| 104 |
|
| 105 |
__POP_FC_STDLIB |