Blame view

mm/maccess.c 1.61 KB
c33fa9f56   Ingo Molnar   uaccess: add prob...
1
2
3
  /*
   * Access kernel memory without faulting.
   */
c33fa9f56   Ingo Molnar   uaccess: add prob...
4
5
  #include <linux/module.h>
  #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
18
19
20
21
  
  long __weak probe_kernel_read(void *dst, void *src, size_t size)
      __attribute__((alias("__probe_kernel_read")));
  
  long __probe_kernel_read(void *dst, void *src, size_t size)
c33fa9f56   Ingo Molnar   uaccess: add prob...
22
23
  {
  	long ret;
b4b8ac524   Jason Wessel   kgdb: fix optiona...
24
  	mm_segment_t old_fs = get_fs();
c33fa9f56   Ingo Molnar   uaccess: add prob...
25

b4b8ac524   Jason Wessel   kgdb: fix optiona...
26
  	set_fs(KERNEL_DS);
c33fa9f56   Ingo Molnar   uaccess: add prob...
27
28
29
30
  	pagefault_disable();
  	ret = __copy_from_user_inatomic(dst,
  			(__force const void __user *)src, size);
  	pagefault_enable();
b4b8ac524   Jason Wessel   kgdb: fix optiona...
31
  	set_fs(old_fs);
c33fa9f56   Ingo Molnar   uaccess: add prob...
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  
  	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.
   */
6144a85a0   Jason Wessel   maccess,probe_ker...
46
47
48
49
  long __weak probe_kernel_write(void *dst, void *src, size_t size)
      __attribute__((alias("__probe_kernel_write")));
  
  long __probe_kernel_write(void *dst, void *src, size_t size)
c33fa9f56   Ingo Molnar   uaccess: add prob...
50
51
  {
  	long ret;
b4b8ac524   Jason Wessel   kgdb: fix optiona...
52
  	mm_segment_t old_fs = get_fs();
c33fa9f56   Ingo Molnar   uaccess: add prob...
53

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