sanitize addresses for non-privileged processes
Subsystems (3)
cover sanitize addresses for non-privileged processes
This RFC makes an attempt to reduce the number of leaking kernel addresses to userspace, in particular to non-privileged processes.
procfs files created using the seq_file interface can benefit from the kptr_restrict sysctl to limit leaking addresses. This RFC presents an alternate approach, sanitizing the addresses as they are printed based on whether the process that opened the proc file had root privileges or not.
We add a boolean flag to the struct seq_file and set it in seq_open() based on the UID/EUID of the current process. Later, when seq_vprint() is called, we check the flag before either printing the address (with vsnprintf()) or printing a sanitized address with [the newly defined] vsnprintf_sanitize().
Patch 1 in the set adds *printf_sanitize() functions to enable patch 2 as just described. Patch 1 adds a fair bit of code since there are so many *printf() variations. Of note, we don't handle vbin_printf() (and kvas_printf() is not touched either).
Is this RFC adding any extra security on top of kptr_restrict==2? I don't know. Is it adding any security to distributions that default to kptr_restrict==0, I think so.
Thanks for taking the time to look at this. I had good fun writing it and learned a bunch, if you don't think it is worth pursuing any further please don't be shy to say so.
Tobin C. Harding (2):
printk: add sanitized versions of *printf()
seq_file: sanitize for non-privileged processes
fs/seq_file.c | 13 +- include/linux/kernel.h | 11 ++ include/linux/seq_file.h | 1 + lib/vsprintf.c | 301 ++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 268 insertions(+), 58 deletions(-)
Signature · 1 line
2.7.4
RFC 1/2 printk: add sanitized versions of *printf()
Kernel addresses should not be leaked to user space.
Add 'sanitized' versions of the *printf() functions that output zeros for any address printed using %pK.
This patch is not a merge candidate, hence the overloading of %pK.
--- include/linux/kernel.h | 11 ++ lib/vsprintf.c | 301 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 255 insertions(+), 57 deletions(-)
include/linux/kernel.h +11 -0
@@ -459,6 +459,17 @@ char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); extern __printf(2, 0) const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args); +extern __printf(2, 3) int sprintf_sanitize(char *buf, const char *fmt, ...); +extern __printf(2, 0) int vsprintf_sanitize(char *buf, const char *, va_list); +extern __printf(3, 4) +int snprintf_sanitize(char *buf, size_t size, const char *fmt, ...); +extern __printf(3, 0) +int vsnprintf_sanitize(char *buf, size_t size, const char *fmt, va_list args); +extern __printf(3, 4) +int scnprintf_sanitize(char *buf, size_t size, const char *fmt, ...); +extern __printf(3, 0) +int vscnprintf_sanitize(char *buf, size_t size, const char *fmt, va_list args); + extern __scanf(2, 3) int sscanf(const char *, const char *, ...); extern __scanf(2, 0)
lib/vsprintf.c +244 -57
403 lines not on this page · raw message
Signature · 1 line
2.7.4
RFC 2/2 seq_file: sanitize for non-privileged processes
In reply to Tobin C. HardingKernel addresses should not be leaked to user space. Currently the only mechanism we have to restrict kernel addresses from leaking is the sysctl kptr_restrict. We don't need to rely on this mechanism, we can sanitize kernel addresses in seq_files whenever a non-privileged process attempts to show them.
Call vsnprintf_sanitize() for non-privileged processes.
--- fs/seq_file.c | 13 ++++++++++++- include/linux/seq_file.h | 1 + 2 files changed, 13 insertions(+), 1 deletion(-)
fs/seq_file.c +12 -1
@@ -46,6 +46,7 @@ static void *seq_buf_alloc(unsigned long size)*/ int seq_open (struct file *file,const struct seq_operations *op) { +const struct cred *cred =current_cred ();struct seq_file *p;WARN_ON (file->private_data);
@@ -80,6 +81,12 @@ int seq_open(struct file *file, const struct seq_operations *op)* file.open() which calls seq_open() and then sets FMODE_PWRITE. */ file->f_mode &= ~FMODE_PWRITE ; + + p->sanitize = true; +if (uid_eq (cred->uid,GLOBAL_ROOT_UID ) || +uid_eq (cred->euid,GLOBAL_ROOT_UID )) { + p->sanitize = false; + }return 0 ; }EXPORT_SYMBOL (seq_open);
@@ -391,9 +398,13 @@ EXPORT_SYMBOL(seq_escape);void seq_vprintf (struct seq_file *m,const char *f,va_list args) {int len; +int (*fn)(char *,size_t ,const char *,va_list ) = vsnprintf_sanitize; + +if (m->sanitize == false) + fn = vsnprintf;if (m->count < m->size) { - len =vsnprintf (m->buf + m->count, m->size - m->count, f, args); + len =fn (m->buf + m->count, m->size - m->count, f, args);if (m->count + len < m->size) { m->count += len;return ;
include/linux/seq_file.h +1 -0
@@ -25,6 +25,7 @@ struct seq_file {const struct seq_operations *op;int poll_event;const struct file *file; +bool sanitize;void *private; };
Signature · 1 line
2.7.4