Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/fstat Provide a helpful error message if we don't ha...



details:   https://anonhg.NetBSD.org/src/rev/28add4b87940
branches:  trunk
changeset: 943109:28add4b87940
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Aug 26 23:08:29 2020 +0000

description:
Provide a helpful error message if we don't have privs to read kernel
addresses.

diffstat:

 usr.bin/fstat/fstat.c |  24 ++++++++++++++++++++++--
 1 files changed, 22 insertions(+), 2 deletions(-)

diffs (59 lines):

diff -r b6e687824b7b -r 28add4b87940 usr.bin/fstat/fstat.c
--- a/usr.bin/fstat/fstat.c     Wed Aug 26 23:08:26 2020 +0000
+++ b/usr.bin/fstat/fstat.c     Wed Aug 26 23:08:29 2020 +0000
@@ -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 @@
 #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    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 @@
                        usage();
                }
 
+       check_privs();
+
        kdriver_init();
 
        if (*(argv += optind)) {
@@ -309,6 +312,23 @@
        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;
 



Home | Main Index | Thread Index | Old Index