The read () system call returns a greater number than count

I read the data from /proc/<pid>/environand got fancy results. How is this possible?

open("/proc/24696/environ", O_RDONLY)   = 10
read(10, "24694\nPPid:\t2606\nTracerPid:\t0\nUi"..., 4096) = 1470144576

open("/proc/25387/environ", O_RDONLY)   = 10
read(10, "686\nPPid:\t1\nTracerPid:\t0\nUid:\t10"..., 4096) = 5905728

Why readreturns a larger value than count(4096)?

This does not happen every time.

Debian 7, 3.2.0-4-amd64 # 1 SMP Debian 3.2.84-1 x86_64 GNU / Linux

Update:

I do not think this is a mistake strace. I also print the result of the glibc function readwith the same result.

Update 2:

I created a test application that reproduces the problem, https://gist.github.com/lstipakov/70c5b5e96112c7f1f6204d70b2c8280e

It lists all processes in /procand reads from a file environ. The problem is reproduced in less than a minute:

// do read, which sometimes returns weird values on 3.2.0-4-amd64 #1 SMP Debian 3.2.84-1 x86_64 GNU/Linux
char tmp[4096];
auto val = read(fd, tmp, sizeof(tmp));
if (val > sizeof(tmp)) {
    std::cout << path << " read " << val << std::endl;
}

CXXFLAGS = "- std = ++ 11" make proc && & sudo./proc

:

/proc/24341/environ 812785856

openvpn :

$ while true; do make clean; make; done
+4

Source: https://habr.com/ru/post/1668492/


All Articles