#
# To hide the processes, adjust the 'kern.seeotheruids' setting.
# echo 'kern.seeotheruids=0' >> /etc/sysctl.conf
#
# To apply this patch:
# doas git clone -b master --depth=1 https://github.com/openbsd/src.git /usr/src
# cd /usr/src && doas git apply /tmp/obsd_seeotheruids.diff
# cd /usr/src/include && doas make includes
# cd /usr/src/lib/libc && doas make -j8 && doas make install
# cd /usr/src/sbin/sysctl && doas make && doas make install
# cd /usr/src/sys/arch/amd64/compile/GENERIC.MP && doas make -j8 && doas make 
install
# reboot the system.
#
diff --git a/lib/libc/sys/sysctl.2 b/lib/libc/sys/sysctl.2
index 4f78c121a..c7a324e5d 100644
--- a/lib/libc/sys/sysctl.2
+++ b/lib/libc/sys/sysctl.2
@@ -515,6 +515,10 @@ When running with a
 .Xr securelevel 7
 greater than 0,
 this variable may not be changed.
+.It Dv KERN_SEEOTHERUIDS Pq Va kern.seeotheruids
+When set to zero, the list of current processes will
+be restricted to those owned by the current user, while
+only root will have visibility over all processes.
 .It Dv KERN_ARGMAX Pq Va kern.argmax
 The maximum number of bytes allowed among the arguments to
 .Xr execve 2 .
diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c
index a2d2f2e06..88e7fad15 100644
--- a/sys/kern/kern_sysctl.c
+++ b/sys/kern/kern_sysctl.c
@@ -129,6 +129,7 @@ extern int video_record_enable;
 extern int autoconf_serial;
 
 int allowkmem;
+int seeotheruids = 1;
 
 int sysctl_diskinit(int, struct proc *);
 int sysctl_proc_args(int *, u_int, void *, size_t *, struct proc *);
@@ -649,6 +650,13 @@ kern_sysctl(int *name, u_int namelen, void *oldp, size_t 
*oldlenp, void *newp,
                return (timeout_sysctl(oldp, oldlenp, newp, newlen));
        case KERN_UTC_OFFSET:
                return (sysctl_utc_offset(oldp, oldlenp, newp, newlen));
+       case KERN_SEEOTHERUIDS: {
+               if (securelevel > 0)
+                       return (sysctl_rdint(oldp, oldlenp, newp,
+                                               seeotheruids));
+               return (sysctl_int(oldp, oldlenp, newp, newlen,
+                                       &seeotheruids));
+                               }
        default:
                return (sysctl_bounded_arr(kern_vars, nitems(kern_vars), name,
                    namelen, oldp, oldlenp, newp, newlen));
@@ -1610,6 +1618,7 @@ sysctl_doproc(int *name, u_int namelen, char *where, 
size_t *sizep)
        int error, needed, op;
        int dothreads = 0;
        int show_pointers;
+       int euid, is_suser, show_otheruids;
 
        dp = where;
        buflen = where != NULL ? *sizep : 0;
@@ -1627,6 +1636,9 @@ sysctl_doproc(int *name, u_int namelen, char *where, 
size_t *sizep)
        op &= ~KERN_PROC_SHOW_THREADS;
 
        show_pointers = suser(curproc) == 0;
+       is_suser = suser(curproc) == 0;
+       show_otheruids = seeotheruids || is_suser;
+       euid = curproc->p_ucred->cr_uid;
 
        if (where != NULL)
                kproc = malloc(sizeof(*kproc), M_TEMP, M_WAITOK);
@@ -1638,6 +1650,9 @@ again:
                /* XXX skip processes in the middle of being zapped */
                if (pr->ps_pgrp == NULL)
                        continue;
+               /* XXX skip processes that does not belong to us */
+               if (!show_otheruids && pr->ps_ucred->cr_uid != euid)
+                       continue;
 
                /*
                 * Skip embryonic processes.
diff --git a/sys/sys/sysctl.h b/sys/sys/sysctl.h
index 083b2ae21..ae30516e8 100644
--- a/sys/sys/sysctl.h
+++ b/sys/sys/sysctl.h
@@ -193,7 +193,8 @@ struct ctlname {
 #define        KERN_VIDEO              89      /* struct: video properties */
 #define        KERN_CLOCKINTR          90      /* node: clockintr */
 #define        KERN_AUTOCONF_SERIAL    91      /* int: kernel device tree 
state serial */
-#define        KERN_MAXID              92      /* number of valid kern ids */
+#define        KERN_SEEOTHERUIDS       92      /* view processes belonging to 
other users */
+#define        KERN_MAXID              93      /* number of valid kern ids */
 
 #define        CTL_KERN_NAMES { \
        { 0, 0 }, \
@@ -288,6 +289,7 @@ struct ctlname {
        { "video", CTLTYPE_STRUCT }, \
        { "clockintr", CTLTYPE_NODE }, \
        { "autoconf_serial", CTLTYPE_INT }, \
+       { "seeotheruids", CTLTYPE_INT }, \
 }
 
 /*

Reply via email to