Blame view

mm/maccess.c 1.63 KB
c33fa9f56   Ingo Molnar   uaccess: add prob...
1
2
3
  /*
   * Access kernel memory without faulting.
   */
b95f1b31b   Paul Gortmaker   mm: Map most file...
4
  #include <linux/export.h>
c33fa9f56   Ingo Molnar   uaccess: add prob...
5
  #include <linux/mm.h>
7c7fcf762   David Howells   MN10300: Save fra...
6
  #include <linux/uaccess.h>
c33fa9f56   Ingo Molnar   uaccess: add prob...
7
8
9
10
11
12
13
14
15
16
  
  /**
   * probe_kernel_read(): safely attempt to read from a location
   * @dst: pointer to the buffer that shall take the data
   * @src: address to read from
   * @size: size of the data chunk
   *
   * Safely read from address @src to the buffer at @dst.  If a kernel fault
   * happens, handle that and return -EFAULT.
   */
6144a85a0   Jason Wessel   maccess,probe_ker...
17

f29c50419   Steven Rostedt   maccess,probe_ker...
18
  long __weak probe_kernel_read(void *dst, const void *src, size_t size)
6144a85a0   Jason Wessel   maccess,probe_ker...
19
      __attribute__((alias("__probe_kernel_read")));
f29c50419   Steven Rostedt   maccess,probe_ker...
20
  long __probe_kernel_read(void *dst, const void *src, size_t size)
c33fa9f56   Ingo Molnar   uaccess: add prob...
21
22
  {
  	long ret;
b4b8ac524   Jason Wessel   kgdb: fix optiona...
23
  	mm_segment_t old_fs = get_fs();
c33fa9f56   Ingo Molnar   uaccess: add prob...
24

b4b8ac524   Jason Wessel   kgdb: fix optiona...
25
  	set_fs(KERNEL_DS);
c33fa9f56   Ingo Molnar   uaccess: add prob...
26
27
28
29
  	pagefault_disable();
  	ret = __copy_from_user_inatomic(dst,
  			(__force const void __user *)src, size);
  	pagefault_enable();
b4b8ac524   Jason Wessel   kgdb: fix optiona...
30
  	set_fs(old_fs);
c33fa9f56   Ingo Molnar   uaccess: add prob...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  
  	return ret ? -EFAULT : 0;
  }
  EXPORT_SYMBOL_GPL(probe_kernel_read);
  
  /**
   * probe_kernel_write(): safely attempt to write to a location
   * @dst: address to write to
   * @src: pointer to the data that shall be written
   * @size: size of the data chunk
   *
   * Safely write to address @dst from the buffer at @src.  If a kernel fault
   * happens, handle that and return -EFAULT.
   */
f29c50419   Steven Rostedt   maccess,probe_ker...
45
  long __weak probe_kernel_write(void *dst, const void *src, size_t size)
6144a85a0   Jason Wessel   maccess,probe_ker...
46
      __attribute__((alias("__probe_kernel_write")));
f29c50419   Steven Rostedt   maccess,probe_ker...
47
  long __probe_kernel_write(void *dst, const void *src, size_t size)
c33fa9f56   Ingo Molnar   uaccess: add prob...
48
49
  {
  	long ret;
b4b8ac524   Jason Wessel   kgdb: fix optiona...
50
  	mm_segment_t old_fs = get_fs();
c33fa9f56   Ingo Molnar   uaccess: add prob...
51

b4b8ac524   Jason Wessel   kgdb: fix optiona...
52
  	set_fs(KERNEL_DS);
c33fa9f56   Ingo Molnar   uaccess: add prob...
53
54
55
  	pagefault_disable();
  	ret = __copy_to_user_inatomic((__force void __user *)dst, src, size);
  	pagefault_enable();
b4b8ac524   Jason Wessel   kgdb: fix optiona...
56
  	set_fs(old_fs);
c33fa9f56   Ingo Molnar   uaccess: add prob...
57
58
59
60
  
  	return ret ? -EFAULT : 0;
  }
  EXPORT_SYMBOL_GPL(probe_kernel_write);