Skip to content

Commit 22ec1a2

Browse files
keesgregkh
authored andcommitted
/dev/mem: Add bounce buffer for copy-out
As done for /proc/kcore in commit df04abf ("fs/proc/kcore.c: Add bounce buffer for ktext data") this adds a bounce buffer when reading memory via /dev/mem. This is needed to allow kernel text memory to be read out when built with CONFIG_HARDENED_USERCOPY (which refuses to read out kernel text) and without CONFIG_STRICT_DEVMEM (which would have refused to read any RAM contents at all). Since this build configuration isn't common (most systems with CONFIG_HARDENED_USERCOPY also have CONFIG_STRICT_DEVMEM), this also tries to inform Kconfig about the recommended settings. This patch is modified from Brad Spengler/PaX Team's changes to /dev/mem code in the last public patch of grsecurity/PaX based on my understanding of the code. Changes or omissions from the original code are mine and don't reflect the original grsecurity/PaX code. Reported-by: Michael Holzheu <holzheu@linux.vnet.ibm.com> Fixes: f5509cc ("mm: Hardened usercopy") Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent d9cc5a0 commit 22ec1a2

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

‎drivers/char/mem.c‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ static ssize_t read_mem(struct file *file, char __user *buf,
107107
phys_addr_t p = *ppos;
108108
ssize_t read, sz;
109109
void *ptr;
110+
char *bounce;
111+
int err;
110112

111113
if (p != *ppos)
112114
return 0;
@@ -129,15 +131,22 @@ static ssize_t read_mem(struct file *file, char __user *buf,
129131
}
130132
#endif
131133

134+
bounce = kmalloc(PAGE_SIZE, GFP_KERNEL);
135+
if (!bounce)
136+
return -ENOMEM;
137+
132138
while (count > 0) {
133139
unsigned long remaining;
134140
int allowed;
135141

136142
sz = size_inside_page(p, count);
137143

144+
err = -EPERM;
138145
allowed = page_is_allowed(p >> PAGE_SHIFT);
139146
if (!allowed)
140-
return -EPERM;
147+
goto failed;
148+
149+
err = -EFAULT;
141150
if (allowed == 2) {
142151
/* Show zeros for restricted memory. */
143152
remaining = clear_user(buf, sz);
@@ -149,24 +158,32 @@ static ssize_t read_mem(struct file *file, char __user *buf,
149158
*/
150159
ptr = xlate_dev_mem_ptr(p);
151160
if (!ptr)
152-
return -EFAULT;
153-
154-
remaining = copy_to_user(buf, ptr, sz);
161+
goto failed;
155162

163+
err = probe_kernel_read(bounce, ptr, sz);
156164
unxlate_dev_mem_ptr(p, ptr);
165+
if (err)
166+
goto failed;
167+
168+
remaining = copy_to_user(buf, bounce, sz);
157169
}
158170

159171
if (remaining)
160-
return -EFAULT;
172+
goto failed;
161173

162174
buf += sz;
163175
p += sz;
164176
count -= sz;
165177
read += sz;
166178
}
179+
kfree(bounce);
167180

168181
*ppos += read;
169182
return read;
183+
184+
failed:
185+
kfree(bounce);
186+
return err;
170187
}
171188

172189
static ssize_t write_mem(struct file *file, const char __user *buf,

‎security/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ config HARDENED_USERCOPY
143143
bool "Harden memory copies between kernel and userspace"
144144
depends on HAVE_HARDENED_USERCOPY_ALLOCATOR
145145
select BUG
146+
imply STRICT_DEVMEM
146147
help
147148
This option checks for obviously wrong memory regions when
148149
copying memory to/from the kernel (via copy_to_user() and

0 commit comments

Comments
 (0)