I read data from /proc/<pid>/environ
and got bizarre results. How this could be 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
How come read
returns bigger value than count
(4096) ?
This does not happen every time, though.
Debian 7, 3.2.0-4-amd64 #1 SMP Debian 3.2.84-1 x86_64 GNU/Linux
Update:
I don't think it is strace
bug - I also print result of glibc's read
function with same result.
Update 2:
I created a test app which reproduces problem, https://gist.github.com/lstipakov/70c5b5e96112c7f1f6204d70b2c8280e
It enumerates all processes under /proc
and reads from environ
file. Problem reproduces in less than 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=c++11" make proc && sudo ./proc
after few seconds:
/proc/24341/environ read 812785856
Could be reproduced by compiling openvpn in a loop:
$ while true; do make clean; make; done