Let's enhance portability and readability slightly.
Signed-off-by: Karel Zak <kzak@redhat.com>
stdint.h \
stdio_ext.h \
stdlib.h \
+ sys/auxv.h \
sys/disk.h \
sys/disklabel.h \
sys/endian.h \
fsopen \
fspick \
fsync \
+ getauxval \
getdomainname \
getdtablesize \
getexecname \
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
+#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <stdarg.h>
# define NAME_MAX PATH_MAX
#endif
+#ifndef HAVE_SYS_AUXV_H
+# include <sys/auxv.h>
+#endif
+
#define BIT(n) (1 << (n))
/*
return errno ? -errno : -1;
}
+static inline bool is_privileged_execution(void)
+{
+#if defined(HAVE_GETAUXVAL) && defined(AT_SECURE)
+ return getauxval(AT_SECURE) != 0;
+#else
+ return (geteuid() != getuid()) || (getegid() != getgid());
+#endif
+}
+
/*
* The usleep function was marked obsolete in POSIX.1-2001 and was removed
* in POSIX.1-2008. It was replaced with nanosleep() that provides more
#include <stdarg.h>
#include <string.h>
-#include <sys/auxv.h> // for getauxval()
+
+#include "c.h"
struct ul_debug_maskname {
const char *name;
} else \
lib ## _debug_mask = mask; \
if (lib ## _debug_mask) { \
- if (getauxval(AT_SECURE)) { \
+ if (is_privileged_execution()) { \
lib ## _debug_mask |= __UL_DEBUG_FL_NOADDR; \
fprintf(stderr, "%d: %s: don't print memory addresses (SUID executable).\n", getpid(), # lib); \
} \
#include <sys/syscall.h>
#endif
#include <unistd.h>
-#include <sys/auxv.h> // for getauxval()
#include <sys/types.h>
#include "env.h"
char *safe_getenv(const char *arg)
{
- if (getauxval(AT_SECURE))
+ if (is_privileged_execution())
return NULL;
#ifdef HAVE_PRCTL
if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
#include "match.h"
#include <stdarg.h>
-#include <sys/auxv.h> // for getauxval()
#include <sys/wait.h>
#include "mount-api-utils.h"
INIT_LIST_HEAD(&cxt->hooksets_datas);
/* if we're really root and aren't running setuid */
- cxt->restricted = (uid_t) 0 == ruid && !getauxval(AT_SECURE) ? 0 : 1;
+ cxt->restricted = (uid_t) 0 == ruid && !is_privileged_execution() ? 0 : 1;
cxt->noautofs = 0;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/auxv.h> // for getauxval()
#include <sys/types.h>
#include <unistd.h>
int broken = 0;
/* real root does not have restrictions */
- if (!getauxval(AT_SECURE) && getuid() == 0) {
+ if (!is_privileged_execution() && getuid() == 0) {
ctl->allow_fullname = ctl->allow_room = ctl->allow_work = ctl->allow_home = 1;
return;
}
#ifdef HAVE_LIBUSER
/* If we're setuid and not really root, disallow the password change. */
- if (getauxval(AT_SECURE) && uid != ctl.pw->pw_uid) {
+ if (is_privileged_execution() && uid != ctl.pw->pw_uid) {
#else
if (uid != 0 && uid != ctl.pw->pw_uid) {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/auxv.h> // for getauxval()
#include <sys/types.h>
#include <unistd.h>
/* reality check */
#ifdef HAVE_LIBUSER
/* If we're setuid and not really root, disallow the password change. */
- if (getauxval(AT_SECURE) && uid != pw->pw_uid) {
+ if (is_privileged_execution() && uid != pw->pw_uid) {
#else
if (uid != 0 && uid != pw->pw_uid) {
#endif
*/
#include <stdio.h>
#include <getopt.h>
-#include <sys/auxv.h> // for getauxval()
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
const uid_t ruid = getuid();
/* if we're really root and aren't running setuid */
- return (uid_t) 0 == ruid && !getauxval(AT_SECURE) ? 0 : 1;
+ return (uid_t) 0 == ruid && !is_privileged_execution() ? 0 : 1;
}
/* Don't rely on PAM and reset the most important limits. */
security/pam_appl.h
security/pam_misc.h
security/pam_modules.h
+ sys/auxv.h
sys/disk.h
sys/disklabel.h
sys/endian.h
fsync
getttynam
utimensat
+ getauxval
getdomainname
getdtablesize
getexecname
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
-#include <sys/auxv.h> // for getauxval()
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
return -1;
case 0: /* child */
- if (getauxval(AT_SECURE) && drop_permissions() != 0)
+ if (is_privileged_execution() && drop_permissions() != 0)
exit(EXIT_FAILURE);
cmd[idx++] = "mkswap";
*
*/
-#include <sys/auxv.h> // for getauxval()
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
* After all, our invoker can easily do "wall < file"
* instead of "wall file".
*/
- uid_t uid = getuid();
- if (uid && getauxval(AT_SECURE))
+ if (getuid() && is_privileged_execution())
errx(EXIT_FAILURE, _("will not read %s - use stdin."),
fname);
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
-#include <sys/auxv.h> // for getauxval()
#include <sys/param.h>
#include <ctype.h>
#include <signal.h>
}
va_end(argp);
- if (getauxval(AT_SECURE)
- && drop_permissions() != 0)
+ if (is_privileged_execution() && drop_permissions() != 0)
err(EXIT_FAILURE, _("drop permissions failed"));
execvp(cmd, args);