Use MMAP/dev/MEM to read and write Linux memory-root permission

Source: Internet
Author: User
Use MMAP/dev/MEM to read and write Linux memory
Category:
Linux Kernel

114 reading comments (1)
Favorites
Report

Use hexedit/dev/MEM to display information in all physical memory. Use MMAP to map/dev/MEM, and then directly read and write it to implement kernel operations in the user space.

Below is a sample I wrote

View plain
  1. # Include <stdio. h>
  2. # Include <unistd. h>
  3. # Include <sys/Mman. h>
  4. # Include <sys/types. h>
  5. # Include <sys/STAT. h>
  6. # Include <fcntl. h>
  7. Int main ()
  8. {
  9. Unsigned char * map_base;
  10. File * F;
  11. Int N, FD;
  12. FD = open ("/dev/mem", o_rdwr | o_sync );
  13. If (FD =-1)
  14. {
  15. Return (-1 );
  16. }
  17. Map_base = MMAP (null, 0xff, prot_read | prot_write, map_shared, FD, 0x20000 );
  18. If (map_base = 0)
  19. {
  20. Printf ("null pointer! \ N ");
  21. }
  22. Else
  23. {
  24. Printf ("successfull! \ N ");
  25. }
  26. Unsigned long ADDR;
  27. Unsigned char content;
  28. Int I = 0;
  29. For (; I <0xff; ++ I)
  30. {
  31. ADDR = (unsigned long) (map_base + I );
  32. Content = map_base [I];
  33. Printf ("Address: 0x % lx content 0x % x \ t", ADDR, (unsigned INT) content );
  34. Map_base [I] = (unsigned char) I;
  35. Content = map_base [I];
  36. Printf ("updated address: 0x % lx content 0x % x \ n", ADDR, (unsigned INT) content );
  37. }
  38. Close (FD );
  39. Munmap (map_base, 0xff );
  40. Return (1 );
  41. }

In the preceding example, the starting address 0x20000 (physical address) is mapped to 0xff. Then you can operate the memory like an ordinary array.

The output result is as follows:

Address: 0x7f3f95391000 content 0x0 updated address: 0x7f3f95391000 content 0x0
Address: 0x7f3f95391001 content 0x0 updated address: 0x7f3f95391001 content 0x1
Address: 0x7f3f95391002 content 0x0 updated address: 0x7f3f95391002 content 0x2
Address: 0x7f3f95391003 content 0x0 updated address: 0x7f3f95391003 content 0x3
Address: 0x7f3f95391004 content 0x0 updated address: 0x7f3f95391004 content 0x4
...

My testing machine is a 64-bit machine. In this example, the physical address 0x20000 is mapped to the virtual address 0x7f3f95391000.

First, output the content under the current address, and then write a new value.

You can use hexedit/dev/MEM to verify that the new value has been written.

This can be done if you want to process the address allocated by the kernel in user mode. First, use pai_addr = get_free_pages (gfp_kernel, order) to allocate memory, use phy_addr = _ Pa (pai_addr) to obtain the physical address, and then map/dev/MEM with MMAP in the user State, offset is phy_addr, and length is set to 2 ^ order. Now you can read and write the memory allocated by the kernel in user mode.

Note: This operation requires the root permission.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.