From f55490be1e5fbcf6f6992212626307381c24890f Mon Sep 17 00:00:00 2001 From: christos Date: Wed, 26 Aug 2020 23:08:29 +0000 Subject: [PATCH] Provide a helpful error message if we don't have privs to read kernel addresses. --- usr.bin/fstat/fstat.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/usr.bin/fstat/fstat.c b/usr.bin/fstat/fstat.c index f4add4aa544b..6a4c2b2512e4 100644 --- a/usr.bin/fstat/fstat.c +++ b/usr.bin/fstat/fstat.c @@ -1,4 +1,4 @@ -/* $NetBSD: fstat.c,v 1.113 2019/09/06 17:08:22 christos Exp $ */ +/* $NetBSD: fstat.c,v 1.114 2020/08/26 23:08:29 christos Exp $ */ /*- * Copyright (c) 1988, 1993 @@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 1993\ #if 0 static char sccsid[] = "@(#)fstat.c 8.3 (Berkeley) 5/2/95"; #else -__RCSID("$NetBSD: fstat.c,v 1.113 2019/09/06 17:08:22 christos Exp $"); +__RCSID("$NetBSD: fstat.c,v 1.114 2020/08/26 23:08:29 christos Exp $"); #endif #endif /* not lint */ @@ -181,6 +181,7 @@ static void vtrans(struct file *, struct vnode *, int, int, long); static void ftrans(fdfile_t *, int); static void ptrans(struct file *, struct pipe *, int); static void kdriver_init(void); +static void check_privs(void); int main(int argc, char **argv) @@ -244,6 +245,8 @@ main(int argc, char **argv) usage(); } + check_privs(); + kdriver_init(); if (*(argv += optind)) { @@ -309,6 +312,23 @@ main(int argc, char **argv) return 0; } +static void +check_privs(void) +{ + int expaddr; + size_t expsize = sizeof(expaddr); + const char *expname = "kern.expose_address"; + + if (geteuid() == 0) + return; + + if (sysctlbyname(expname, &expaddr, &expsize, NULL, 0) == -1) + err(EXIT_FAILURE, "Can't get sysctl `%s'", expname); + if (expaddr == 0) + errx(EXIT_FAILURE, "This program does not work without " + "sysctl `%s' being set", expname); +} + static const char *Uname, *Comm; pid_t Pid;