Get pages attribute by system call in Linux - linux

Is there any system call or function that can get page attribute like readable, writable, executable by page address?
I know we can use mprotect to set pages attribute, but I have no idea if we can get pages attribute. Any comment is appreciated.

There isn't. You need to use the proc interface, which contains lots of information about each process in various files. The information you're looking for is in plaintext, in /proc/<pid>/maps. You can take a look at it for your current process by running:
$ cat /proc/$$/maps
You can find some more information in man 5 proc.
The example given at the above link is:
address perms offset dev inode pathname
08048000-08056000 r-xp 00000000 03:0c 64593 /usr/sbin/gpm
08056000-08058000 rw-p 0000d000 03:0c 64593 /usr/sbin/gpm
08058000-0805b000 rwxp 00000000 00:00 0
40000000-40013000 r-xp 00000000 03:0c 4165 /lib/ld-2.2.4.so
40013000-40015000 rw-p 00012000 03:0c 4165 /lib/ld-2.2.4.so
4001f000-40135000 r-xp 00000000 03:0c 45494 /lib/libc-2.2.4.so
40135000-4013e000 rw-p 00115000 03:0c 45494 /lib/libc-2.2.4.so
4013e000-40142000 rw-p 00000000 00:00 0
bffff000-c0000000 rwxp 00000000 00:00 0
As you can see, the permissions are the second (space-delimited) field there. So from a program (like in C, since you mentioned mprotect(), you could open up /proc/$$/maps with fopen(), then use fgets() or scanf() to pull the data out. You're just looking for that perms field of the range where your page lies.

Related

/proc/<pid>/map shows more shared library than ldd for busybox

Why /proc/<pid>/maps shows more shared library than ldd program?
Here's an example:
# ------------ Embedded Linux side ------------
# ps
.....
28 root [jffs2_gcd_mtd2]
132 root -/bin/sh ---> the target to show
155 root [kworker/0:2]
# # Note: -/bin/sh is actually /bin/busybox
# cat /proc/132/maps
00400000-004bc000 r-xp 00000000 1f:02 34 /bin/busybox
004cc000-004cd000 rw-p 000bc000 1f:02 34 /bin/busybox
004cd000-004ee000 rw-p 00000000 00:00 0 [heap]
775bb000-775cb000 rw-p 00000000 00:00 0
775cb000-775d6000 r-xp 00000000 1f:02 397 /lib/libnss_files-2.22.so
775d6000-775e5000 ---p 0000b000 1f:02 397 /lib/libnss_files-2.22.so
775e5000-775e6000 r--p 0000a000 1f:02 397 /lib/libnss_files-2.22.so
775e6000-775e7000 rw-p 0000b000 1f:02 397 /lib/libnss_files-2.22.so
775e7000-775ed000 rw-p 00000000 00:00 0
775ed000-7775a000 r-xp 00000000 1f:02 382 /lib/libc-2.22.so
7775a000-7776a000 ---p 0016d000 1f:02 382 /lib/libc-2.22.so
7776a000-7776d000 r--p 0016d000 1f:02 382 /lib/libc-2.22.so
7776d000-77770000 rw-p 00170000 1f:02 382 /lib/libc-2.22.so
77770000-77772000 rw-p 00000000 00:00 0
77772000-77795000 r-xp 00000000 1f:02 374 /lib/ld-2.22.so
777a1000-777a2000 rw-p 00000000 00:00 0
777a3000-777a4000 rw-p 00000000 00:00 0
777a4000-777a5000 r--p 00022000 1f:02 374 /lib/ld-2.22.so
777a5000-777a6000 rw-p 00023000 1f:02 374 /lib/ld-2.22.so
7fb42000-7fb63000 rwxp 00000000 00:00 0 [stack]
7fff7000-7fff8000 r-xp 00000000 00:00 0 [vdso]
# cksum /bin/busybox
698740119 773496 /bin/busybox
#---------- Then on the PC side ----------------
$ cksum ./busybox
698740119 773496 bin/busybox
$ /usr/local/bin/mips-linux-gnu-ldd bin/busybox
checking sub-depends for 'not found'
libc.so.6 => not found (0x00000000)
/lib/ld.so.1 => /lib/ld.so.1 (0x00000000)
cksum is used to check if the files are the same.
From PC side ldd cross-tool, it shows busybox depends on libc and ld only.
However, in the real run-time environment, /proc/132/maps shows one more shared library, /lib/libnss_files-2.22.so.
To confirm if we have indirect dependency:
$ /usr/local/bin/mips-linux-gnu-ldd lib/libc.so.6
/lib/ld.so.1 => /lib/ld.so.1 (0x00000000)
$ /usr/local/bin/mips-linux-gnu-ldd lib/ld.so.1
not a dynamic executable
No. ldd show that libc depends on ld, which is already one of busybox's dependency.
One process can dlopen(3) a shared object and load it in its address space without the need of linking it to it. This means a shared object doesn't need to appear in the ldd(1) output for it to appear in the running instance of the program.
I'm not saying that this is the case, but you can develop shared objects with the only purpose of adding (unknown) functionality to a program that had open design to incorporate libraries on demand.
Normally, those programs don't appear in the ldd output.
libnss_files is a component of libc. It's loaded on demand, and is used to perform name lookups in static files, like /etc/passwd and /etc/hosts. (There's also libnss_dns for DNS lookups.)
For more information, see the libc documentation for NSS (Name Service Switch).

Retrieving the memory map of its own process in QNX

In Linux if we look at the /proc/self/maps:
00400000-004ef000 r-xp 00000000 08:01 12845058 /bin/bash
006ef000-006f0000 r--p 000ef000 08:01 12845058 /bin/bash
006f0000-006f9000 rw-p 000f0000 08:01 12845058 /bin/bash
006f9000-006ff000 rw-p 00000000 00:00 0
00d5a000-010a2000 rw-p 00000000 00:00 0 [heap]
7f6fe582a000-7f6fe5835000 r-xp 00000000 08:01 1048595 /lib/x86_64-linux-gnu/libnss_files-2.19.so
7f6fe5835000-7f6fe5a34000 ---p 0000b000 08:01 1048595 /lib/x86_64-linux-gnu/libnss_files-2.19.so
7f6fe5a34000-7f6fe5a35000 r--p 0000a000 08:01 1048595 /lib/x86_64-linux-gnu/libnss_files-2.19.so
7f6fe5a35000-7f6fe5a36000 rw-p 0000b000 08:01 1048595 /lib/x86_64-linux-gnu/libnss_files-2.19.so
I want information like pathname and starting address for Qnx. How can I get that information?
My analysis says that there is not /proc/self/maps file in Qnx.
Try to use this:
pidin -p<process> mapinfo
We have self file in QNX too !!
The address space is represented by a binary file called "as" in every folder in /proc. /proc/self/as contains the address space of the current process.
Problem is that its not human readable directly. You need to use devctl() to read it. Below is the link for all the devctl commands regarding address space decoding.
http://www.qnx.com/developers/docs/6.5.0_sp1/index.jsp?topic=%2Fcom.qnx.doc.neutrino_cookbook%2Fs3_procfs.html
Example -
devctl("/proc/self/as",DCMD_PROC_TIDSTATUS,&debug_data, sizeof(debug_data)
This command will give you debug data of the thread in a struct _debug_thread_info structure.
Similarly DCMD_PROC_PAGEDATA and DCMD_PROC_MAPINFO can be used to get the segment mapping ( in linux maps file does this) information of a process's address space.
Hope this helps.

virtual memory in linux

I am debugging one issue where a same program behaves differently on different Linux boxes (all 2.6 kernels). Basically on some linux box, mmap() of 16MB always succeeds, but on other linux box the same mmap() would fail with "ENOMEM".
I checked /proc//maps and saw the virtual memory map on different linux boxes are quite different. One thing is: the address range for heap:
linux box1: the mmap() would return address of 31162000-31164000
linux box2: the mmap() would return address of a9a67000-a9a69000
My question is: for a particular process, how is linux virtual memory arranged? What decides the actual address ranges? Why the mmap() would fail even I can still see some large unused virtual address ranges?
UPDATE: here is an example of address space where mmap() of 16MB would fail:
10024000-10f6b000 rwxp 10024000 00:00 0 [heap]
30000000-3000c000 rw-p 30000000 00:00 0
3000c000-3000d000 r--s 00000000 00:0c 5461 /dev/shm/mymap1
3000d000-3000e000 rw-s 00001000 00:0c 5461 /dev/shm/mymap2
3000e000-30014000 r--s 00000000 00:0c 5463 /dev/shm/mymap3
30014000-300e0000 r--s 00000000 00:0c 5465 /dev/shm/mymap4
300e0000-310e0000 r--s 00000000 00:0b 2554 /dev/mymap5
310e0000-31162000 rw-p 310e0000 00:00 0
31162000-31164000 rw-s 00000000 00:0c 3554306 /dev/shm/mymap6
31164000-312e4000 rw-s 00000000 00:0c 3554307 /dev/shm/mymap7
312e4000-32019000 rw-s 00000000 00:0c 3554309 /dev/shm/mymap8
7f837000-7f84c000 rw-p 7ffeb000 00:00 0 [stack]
in the above example: there are still big space between the last mymap8 and [stack]. But further mmap() of 16MB would fail. My question is: how does Linux decide mmap() base and the allowed range?
Thanks.

identifying glibc mmap areas (VMA's) from a Linux kernel module

I understood When allocating a blocks of memory larger than MMAP_THRESHOLD bytes, the glibc malloc() implementation allocates the memory as a private anonymous mapping using mmap ,and this mmap allocated area wont come as a part of [heap] in linux vma.
So is there any method available to identify all the glibc mmap areas from a linux kernel module.?
example :
One of test program which do malloc greater than MMAP_THRESHOLD many times shows cat /proc/pid/maps output as
00013000-00085000 rw-p 00000000 00:00 0 [heap]
40000000-40016000 r-xp 00000000 00:0c 14107305 /lib/arm-linux-gnueabi/ld-2.13.so
4025e000-4025f000 r--p 00001000 00:0c 14107276 /lib/arm-linux-gnueabi/libdl-2.13.so
4025f000-40260000 rw-p 00002000 00:0c 14107276 /lib/arm-linux-gnueabi/libdl-2.13.so
.....
.....
40260000-40261000 ---p 00000000 00:00 0
40261000-40a60000 rw-p 00000000 00:00 0
40a60000-40a61000 ---p 00000000 00:00 0
40a61000-42247000 rw-p 00000000 00:00 0
beed8000-beef9000 rw-p 00000000 00:00 0 [stack]
In this few are (40a61000-42247000,40261000-40a60000) actually glibc mmap areas,So from a Linux kernel module is there any way to identify this areas ,something like below code which identify stack and heap ?
if (vma->vm_start <= mm->start_brk &&
vma->vm_end >= mm->brk) {
name = "[heap]";
} else if (vma->vm_start <= mm->start_stack &&
vma->vm_end >= mm->start_stack) {
name = "[stack]";
}
I believe you should not dump the memory of your application from a kernel module. You should consider using application checkpointing, see this answer and the Berkley checkpoint restart library
You could also consider using the core dumping facilities inside the kernel.

What do the "---p" permissions in /proc/self/maps mean?

I understand the meaning of rwxps bits. r-xp is for .text. rw-p is for .data/.bss/heap/stack. What is the use of just ---p pages?
For example see this output of cat /proc/self/maps
00400000-0040b000 r-xp 00000000 08:03 827490 /bin/cat
0060b000-0060c000 rw-p 0000b000 08:03 827490 /bin/cat
0060c000-0062d000 rw-p 00000000 00:00 0 [heap]
3819a00000-3819a1e000 r-xp 00000000 08:03 532487 /lib64 ld-2.11.2.so
3819c1d000-3819c1e000 r--p 0001d000 08:03 532487 /lib64/ld-2.11.2.so
3819c1e000-3819c1f000 rw-p 0001e000 08:03 532487 /lib64/ld-2.11.2.so
3819c1f000-3819c20000 rw-p 00000000 00:00 0
3819e00000-3819f70000 r-xp 00000000 08:03 532490 /lib64/libc-2.11.2.so
3819f70000-381a16f000 ---p 00170000 08:03 532490 /lib64/libc-2.11.2.so
381a16f000-381a173000 r--p 0016f000 08:03 532490 /lib64/libc-2.11.2.so
381a173000-381a174000 rw-p 00173000 08:03 532490 /lib64/libc-2.11.2.so
381a174000-381a179000 rw-p 00000000 00:00 0
7fb859c49000-7fb85fa7a000 r--p 00000000 08:03 192261 /usr/lib/locale/locale-archive
7fb85fa7a000-7fb85fa7d000 rw-p 00000000 00:00 0
7fb85fa95000-7fb85fa96000 rw-p 00000000 00:00 0
7fff64894000-7fff648a9000 rw-p 00000000 00:00 0 [stack]
7fff649ff000-7fff64a00000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
According to the man page, it means private (copy on write). No idea what the usefulness of such a mapping is without being able to read/write/execute anything in it, though.
Possibly it is private to libc, allowing it to modify the permissions to access it without a user program accidentally mucking it up.
This is something I've wondered about the specifics of too. It didn't appear until sometime in the last few years, but I'm unsure whether GNU binutils or the glibc dynamic linker (ld-linux.so.2) is responsible for the change.
At first I thought it was a sort of guard region created by the dynamic linker to protect against out of bounds access to a library's data segment, but it makes no sense for it to be so large. It's possible that it's a complete map of the while library file so that the dynamic linker can make it readable again at some time in the future (perhaps during dlopen or dlsym calls) to access ELF metadata that doesn't normally need to be mapped.
In any case, it's nasty bloat, especially on 32-bit machines where virtual address space is a precious resource. It also bloats the kernel page tables, increasing the kernelspace resources used by a process.
P.S. Sorry this isn't really an answer. I know it's just random bits and pieces that might help lead to an answer, but it was way too long for a comment.
Private mapping (MAP_PRIVATE): Modifications to the contents of the mapping are not visible to other processes.
For file mapping they are not carried through to the underlying file. Changes to the contents of the mapping are nevertheless private to each process.
The kernel accomplishes this by using the copy-on-write technique. This means that whenever a process attempts to modify the contents of a page, the kernel first creates a new, separate copy of that page for the process (and adjusts the process’s page tables).
For this reason, a MAP_PRIVATE mapping is sometimes referred to as a private, copy-on-write mapping. (Source: The Linux Programming Interface book)

Resources