1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
/*
* Copyright (c) 2020 Dmitry V. Levin <ldv@altlinux.org>
*
* Handy inline functions and macros providing some convenient functionality
* to libpam and its modules.
*/
#ifndef PAM_INLINE_H
#define PAM_INLINE_H
#include "pam_cc_compat.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
/*
* Evaluates to
* - a syntax error if the argument is 0,
* 0, otherwise.
*/
#define PAM_FAIL_BUILD_ON_ZERO(e_) (sizeof(int[-1 + 2 * !!(e_)]) * 0)
/*
* Evaluates to
* 1, if the given type is known to be a non-array type
* 0, otherwise.
*/
#define PAM_IS_NOT_ARRAY(a_) PAM_IS_SAME_TYPE((a_), &(a_)[0])
/*
* Evaluates to
* - a syntax error if the argument is not an array,
* 0, otherwise.
*/
#define PAM_MUST_BE_ARRAY(a_) PAM_FAIL_BUILD_ON_ZERO(!PAM_IS_NOT_ARRAY(a_))
/*
* Evaluates to
* - a syntax error if the argument is an array,
* 0, otherwise.
*/
#define PAM_MUST_NOT_BE_ARRAY(a_) PAM_FAIL_BUILD_ON_ZERO(PAM_IS_NOT_ARRAY(a_))
/* Evaluates to the number of elements in the specified array. */
#define PAM_ARRAY_SIZE(a_) (sizeof(a_) / sizeof((a_)[0]) + PAM_MUST_BE_ARRAY(a_))
/*
* Zero-extend a signed integer type to unsigned long long.
*/
# define zero_extend_signed_to_ull(v_) \
(sizeof(v_) == sizeof(char) ? (unsigned long long) (unsigned char) (v_) : \
sizeof(v_) == sizeof(short) ? (unsigned long long) (unsigned short) (v_) : \
sizeof(v_) == sizeof(int) ? (unsigned long long) (unsigned int) (v_) : \
sizeof(v_) == sizeof(long) ? (unsigned long long) (unsigned long) (v_) : \
(unsigned long long) (v_))
/*
* Sign-extend an unsigned integer type to long long.
*/
# define sign_extend_unsigned_to_ll(v_) \
(sizeof(v_) == sizeof(char) ? (long long) (signed char) (v_) : \
sizeof(v_) == sizeof(short) ? (long long) (signed short) (v_) : \
sizeof(v_) == sizeof(int) ? (long long) (signed int) (v_) : \
sizeof(v_) == sizeof(long) ? (long long) (signed long) (v_) : \
(long long) (v_))
/*
* Returns NULL if STR does not start with PREFIX,
* or a pointer to the first char in STR after PREFIX.
* The length of PREFIX is specified by PREFIX_LEN.
*/
static inline const char *
pam_str_skip_prefix_len(const char *str, const char *prefix, size_t prefix_len)
{
return strncmp(str, prefix, prefix_len) ? NULL : str + prefix_len;
}
#define pam_str_skip_prefix(str_, prefix_) \
pam_str_skip_prefix_len((str_), (prefix_), sizeof(prefix_) - 1 + PAM_MUST_BE_ARRAY(prefix_))
/*
* Returns NULL if STR does not start with PREFIX
* (ignoring the case of the characters),
* or a pointer to the first char in STR after PREFIX.
* The length of PREFIX is specified by PREFIX_LEN.
*/
static inline const char *
pam_str_skip_icase_prefix_len(const char *str, const char *prefix, size_t prefix_len)
{
return strncasecmp(str, prefix, prefix_len) ? NULL : str + prefix_len;
}
#define pam_str_skip_icase_prefix(str_, prefix_) \
pam_str_skip_icase_prefix_len((str_), (prefix_), sizeof(prefix_) - 1 + PAM_MUST_BE_ARRAY(prefix_))
/*
* Macros to securely erase memory
*/
#ifdef HAVE_MEMSET_EXPLICIT
static inline void pam_overwrite_n(void *ptr, size_t len)
{
if (ptr)
memset_explicit(ptr, '\0', len);
}
#elif defined HAVE_EXPLICIT_BZERO
static inline void pam_overwrite_n(void *ptr, size_t len)
{
if (ptr)
explicit_bzero(ptr, len);
}
#else
static inline void pam_overwrite_n(void *ptr, size_t len)
{
if (ptr) {
ptr = memset(ptr, '\0', len);
__asm__ __volatile__ ("" : : "r"(ptr) : "memory");
}
}
#endif
#define pam_overwrite_string(x) \
do { \
char *xx__ = (x) + PAM_MUST_NOT_BE_ARRAY(x); \
if (xx__) \
pam_overwrite_n(xx__, strlen(xx__)); \
} while(0)
#define pam_overwrite_array(x) pam_overwrite_n(x, sizeof(x) + PAM_MUST_BE_ARRAY(x))
#define pam_overwrite_object(x) pam_overwrite_n(x, sizeof(*(x)) + PAM_MUST_NOT_BE_ARRAY(x))
static inline void
pam_drop_response(struct pam_response *reply, int replies)
{
int reply_i;
for (reply_i = 0; reply_i < replies; ++reply_i) {
if (reply[reply_i].resp) {
pam_overwrite_string(reply[reply_i].resp);
free(reply[reply_i].resp);
}
}
free(reply);
}
static inline char * PAM_FORMAT((printf, 1, 2)) PAM_NONNULL((1)) PAM_ATTRIBUTE_MALLOC
pam_asprintf(const char *fmt, ...)
{
int rc;
char *res;
va_list ap;
va_start(ap, fmt);
rc = vasprintf(&res, fmt, ap);
va_end(ap);
return rc < 0 ? NULL : res;
}
static inline int PAM_FORMAT((printf, 3, 4)) PAM_NONNULL((3))
pam_snprintf(char *str, size_t size, const char *fmt, ...)
{
int rc;
va_list ap;
va_start(ap, fmt);
rc = vsnprintf(str, size, fmt, ap);
va_end(ap);
if (rc < 0 || (unsigned int) rc >= size)
return -1;
return rc;
}
#define pam_sprintf(str_, fmt_, ...) \
pam_snprintf((str_), sizeof(str_) + PAM_MUST_BE_ARRAY(str_), (fmt_), \
##__VA_ARGS__)
static inline int
pam_read_passwords(int fd, int npass, char **passwords)
{
/*
* The passwords array must contain npass preallocated
* buffers of length PAM_MAX_RESP_SIZE + 1.
*/
int rbytes = 0;
int offset = 0;
int i = 0;
char *pptr;
while (npass > 0) {
rbytes = read(fd, passwords[i]+offset, PAM_MAX_RESP_SIZE+1-offset);
if (rbytes < 0) {
if (errno == EINTR) {
continue;
}
break;
}
if (rbytes == 0) {
break;
}
while (npass > 0 &&
(pptr = memchr(passwords[i] + offset, '\0', rbytes)) != NULL) {
++pptr; /* skip the '\0' */
rbytes -= pptr - (passwords[i] + offset);
i++;
offset = 0;
npass--;
if (rbytes > 0) {
if (npass > 0) {
memcpy(passwords[i], pptr, rbytes);
}
pam_overwrite_n(pptr, rbytes);
}
}
offset += rbytes;
}
/* clear up */
if (offset > 0 && npass > 0) {
pam_overwrite_n(passwords[i], offset);
}
return i;
}
static inline int
pam_consttime_streq(const char *userinput, const char *secret) {
volatile const char *u = userinput, *s = secret;
volatile int ret = 0;
do {
ret |= *u ^ *s;
s += !!*s;
} while (*u++ != '\0');
return ret == 0;
}
#endif /* PAM_INLINE_H */
|