/
usercopy.c
310 lines (269 loc) · 6.59 KB
/
usercopy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* linux/mm/usercopy.c
*
* (C) Copyright 2003 Ingo Molnar
*
* Generic implementation of all the user-VM access functions, without
* relying on being able to access the VM directly.
*/
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/highmem.h>
#include <linux/pagemap.h>
#include <linux/smp_lock.h>
#include <linux/ptrace.h>
#include <linux/interrupt.h>
#include <asm/pgtable.h>
#include <asm/uaccess.h>
#include <asm/atomic_kmap.h>
/*
* Get kernel address of the user page and pin it.
*/
static inline struct page *pin_page(unsigned long addr, int write,
pte_t *pte)
{
struct mm_struct *mm = current->mm ? : &init_mm;
struct page *page = NULL;
int ret;
if (addr >= current_thread_info()->addr_limit.seg)
return (struct page *)-1UL;
/*
* Do a quick atomic lookup first - this is the fastpath.
*/
retry:
page = follow_page_pte(mm, addr, write, pte);
if (likely(page != NULL)) {
if (!PageReserved(page))
get_page(page);
return page;
}
if (pte_present(*pte))
return NULL;
/*
* No luck - bad address or need to fault in the page:
*/
/* Release the lock so get_user_pages can sleep */
spin_unlock(&mm->page_table_lock);
/*
* In the context of filemap_copy_from_user(), we are not allowed
* to sleep. We must fail this usercopy attempt and allow
* filemap_copy_from_user() to recover: drop its atomic kmap and use
* a sleeping kmap instead.
*/
if (in_atomic()) {
spin_lock(&mm->page_table_lock);
return NULL;
}
down_read(&mm->mmap_sem);
ret = get_user_pages(current, mm, addr, 1, write, 0, NULL, NULL);
up_read(&mm->mmap_sem);
spin_lock(&mm->page_table_lock);
if (ret <= 0)
return NULL;
/*
* Go try the follow_page again.
*/
goto retry;
}
static inline void unpin_page(struct page *page)
{
put_page(page);
}
/*
* Access another process' address space.
* Source/target buffer must be kernel space,
* Do not walk the page table directly, use get_user_pages
*/
static int rw_vm(unsigned long addr, void *buf, int len, int write)
{
struct mm_struct *mm = current->mm ? : &init_mm;
if (!len)
return 0;
spin_lock(&mm->page_table_lock);
/* ignore errors, just check how much was sucessfully transfered */
while (len) {
struct page *page = NULL;
pte_t pte;
int bytes, offset;
void *maddr;
page = pin_page(addr, write, &pte);
if ((page == (struct page *)-1UL) ||
(!page && !pte_present(pte)))
break;
bytes = len;
offset = addr & (PAGE_SIZE-1);
if (bytes > PAGE_SIZE-offset)
bytes = PAGE_SIZE-offset;
if (page)
maddr = kmap_atomic(page, KM_USER_COPY);
else
/* we will map with user pte
*/
maddr = kmap_atomic_pte(&pte, KM_USER_COPY);
#define HANDLE_TYPE(type) \
case sizeof(type): *(type *)(maddr+offset) = *(type *)(buf); break;
if (write) {
switch (bytes) {
HANDLE_TYPE(char);
HANDLE_TYPE(int);
HANDLE_TYPE(long long);
default:
memcpy(maddr + offset, buf, bytes);
}
} else {
#undef HANDLE_TYPE
#define HANDLE_TYPE(type) \
case sizeof(type): *(type *)(buf) = *(type *)(maddr+offset); break;
switch (bytes) {
HANDLE_TYPE(char);
HANDLE_TYPE(int);
HANDLE_TYPE(long long);
default:
memcpy(buf, maddr + offset, bytes);
}
#undef HANDLE_TYPE
}
kunmap_atomic(maddr, KM_USER_COPY);
if (page)
unpin_page(page);
len -= bytes;
buf += bytes;
addr += bytes;
}
spin_unlock(&mm->page_table_lock);
return len;
}
static int str_vm(unsigned long addr, void *buf0, int len, int copy)
{
struct mm_struct *mm = current->mm ? : &init_mm;
struct page *page;
void *buf = buf0;
if (!len)
return len;
spin_lock(&mm->page_table_lock);
/* ignore errors, just check how much was sucessfully transfered */
while (len) {
int bytes, offset, left, copied;
pte_t pte;
char *maddr;
page = pin_page(addr, copy == 2, &pte);
if ((page == (struct page *)-1UL) ||
(!page && !pte_present(pte))) {
spin_unlock(&mm->page_table_lock);
return -EFAULT;
}
bytes = len;
offset = addr & (PAGE_SIZE-1);
if (bytes > PAGE_SIZE-offset)
bytes = PAGE_SIZE-offset;
if (page)
maddr = kmap_atomic(page, KM_USER_COPY);
else
/* we will map with user pte
*/
maddr = kmap_atomic_pte(&pte, KM_USER_COPY);
if (copy == 2) {
memset(maddr + offset, 0, bytes);
copied = bytes;
left = 0;
} else if (copy == 1) {
left = strncpy_count(buf, maddr + offset, bytes);
copied = bytes - left;
} else {
copied = strnlen(maddr + offset, bytes);
left = bytes - copied;
}
BUG_ON(bytes < 0 || copied < 0);
kunmap_atomic(maddr, KM_USER_COPY);
if (page)
unpin_page(page);
len -= copied;
buf += copied;
addr += copied;
if (left)
break;
}
spin_unlock(&mm->page_table_lock);
return len;
}
/*
* Copies memory from userspace (ptr) into kernelspace (val).
*
* returns # of bytes not copied.
*/
int get_user_size(unsigned int size, void *val, const void *ptr)
{
int ret;
if (unlikely(segment_eq(get_fs(), KERNEL_DS)))
ret = __direct_copy_from_user_inatomic(val, ptr, size);
else
ret = rw_vm((unsigned long)ptr, val, size, 0);
if (ret)
/*
* Zero the rest:
*/
memset(val + size - ret, 0, ret);
return ret;
}
/*
* Copies memory from kernelspace (val) into userspace (ptr).
*
* returns # of bytes not copied.
*/
int put_user_size(unsigned int size, const void *val, void *ptr)
{
if (unlikely(segment_eq(get_fs(), KERNEL_DS)))
return __direct_copy_to_user_inatomic(ptr, val, size);
else
return rw_vm((unsigned long)ptr, (void *)val, size, 1);
}
int copy_str_fromuser_size(unsigned int size, void *val, const void *ptr)
{
int copied, left;
if (unlikely(segment_eq(get_fs(), KERNEL_DS))) {
left = strncpy_count(val, ptr, size);
copied = size - left;
BUG_ON(copied < 0);
return copied;
}
left = str_vm((unsigned long)ptr, val, size, 1);
if (left < 0)
return left;
copied = size - left;
BUG_ON(copied < 0);
return copied;
}
int strlen_fromuser_size(unsigned int size, const void *ptr)
{
int copied, left;
if (unlikely(segment_eq(get_fs(), KERNEL_DS))) {
copied = strnlen(ptr, size) + 1;
BUG_ON(copied < 0);
return copied;
}
left = str_vm((unsigned long)ptr, NULL, size, 0);
if (left < 0)
return 0;
copied = size - left + 1;
BUG_ON(copied < 0);
return copied;
}
int zero_user_size(unsigned int size, void *ptr)
{
int left;
if (unlikely(segment_eq(get_fs(), KERNEL_DS))) {
memset(ptr, 0, size);
return 0;
}
left = str_vm((unsigned long)ptr, NULL, size, 2);
if (left < 0)
return size;
return left;
}
EXPORT_SYMBOL(get_user_size);
EXPORT_SYMBOL(put_user_size);
EXPORT_SYMBOL(zero_user_size);
EXPORT_SYMBOL(copy_str_fromuser_size);
EXPORT_SYMBOL(strlen_fromuser_size);