diff --git a/sys/dev/mem/memdev.c b/sys/dev/mem/memdev.c index 28ed6ebc4e93..37bad1538f7f 100644 --- a/sys/dev/mem/memdev.c +++ b/sys/dev/mem/memdev.c @@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -67,8 +68,14 @@ memopen(struct cdev *dev __unused, int flags, int fmt __unused, { int error = 0; - if (flags & FWRITE) - error = securelevel_gt(td->td_ucred, 0); + if (flags & FREAD) + error = priv_check(td, PRIV_KMEM_READ); + if (flags & FWRITE) { + if (error == 0) + error = priv_check(td, PRIV_KMEM_WRITE); + if (error == 0) + error = securelevel_gt(td->td_ucred, 0); + } return (error); } diff --git a/sys/kern/kern_priv.c b/sys/kern/kern_priv.c index fcd599393be4..204e00bb95e9 100644 --- a/sys/kern/kern_priv.c +++ b/sys/kern/kern_priv.c @@ -141,6 +141,15 @@ priv_check_cred(struct ucred *cred, int priv, int flags) } } + /* + * Writes to kernel memory are a typical root-only operation, + * but non-root users are expected to be able to read it. + */ + if (priv == PRIV_KMEM_READ) { + error = 0; + goto out; + } + /* * Now check with MAC, if enabled, to see if a policy module grants * privilege. diff --git a/sys/sys/priv.h b/sys/sys/priv.h index b984f1a0beb4..de3f3bd03b1c 100644 --- a/sys/sys/priv.h +++ b/sys/sys/priv.h @@ -493,6 +493,12 @@ #define PRIV_RCTL_ADD_RULE 673 #define PRIV_RCTL_REMOVE_RULE 674 +/* + * Kernel memory privileges. + */ +#define PRIV_KMEM_READ 680 /* Read from kernel memory. */ +#define PRIV_KMEM_WRITE 681 /* Write to kernel memory. */ + /* * Track end of privilege list. */