News for hackers. Stuff that matters.
[dead]
Re:#include </etc/shadow> by puzzledobserver on Tuesday December 17, 2019 @12:53AM (#21811042)
Neither of the individual facts is surprising. It is the juxtaposition that is. In 20 years of writing C/C++ code, I had never thought of #including files such as /dev/stdin.

Of course, I know how the #include mechanism works, and what the special device files do, so I can anticipate the results of #include </dev/stdin>, but it is unusual enough that I had to write a small test program to see for myself.

Re:#include &lt;/etc/shadow&gt; by commandersaki on Tuesday December 17, 2019 @02:10AM (#21811371)
I’ve known about it for a very long time after someone pointed it out. I always joke that’s how I write C.

I never thought about it in terms of a security problem.

Re:#include &lt;/etc/shadow&gt; by userbinator on Tuesday December 17, 2019 @01:10AM (#21811114)
It used to be the case that programmers were simply another step up from "power user", and as such would've already become familiar with such aspects of the system. Now, people are "learning to code" with barely any basic computer literacy, and the situation is even worse thanks to opaque locked-down platforms like mobile, so that a lot of them unfortunately have only a vague notion of what a file is, much less the whole "devices are also files" paradigm that underlies Unix (and to a certain extent, Windows --- but how many Windows programmers know about CON?)

I've surprised some coworkers -- ones who are definitely not inexperienced or just learning -- by e.g. using /dev/stdout as a logfile, or even /dev/pts/x to have multiple logs directly written to separate terminal windows. They all "knew" that devices are files, but never thought about actually trying to use them as such.

Related: https://news.ycombinator.com/item?id=18349847

Re:#include &lt;/etc/shadow&gt; by adwn on Tuesday December 17, 2019 @12:47AM (#21811015)
> Why is this surprising? Do programmers not read books anymore? [...]

No need to be condescending. Three reasons:

1) A sophisticated compiler might read the length of a file before loading it, so that it can allocate a buffer of the right size. Doesn't work with /dev/stdin.

2) Alternatively, it might read the file via mmap(). Doesn't work with /dev/stdin.

3) Furthermore, it might check whether the file is a regular file. If not, it is almost certainly not what the programmer had in mind.

Re:#include &lt;/etc/shadow&gt; by zonidjan on Tuesday December 17, 2019 @03:04AM (#21811611)
> 1) A sophisticated compiler might read the length of a file before loading it, so that it can allocate a buffer of the right size. Doesn't work with /dev/stdin.

gcc does this. It appears to be why /proc files don't work. gcc sees that stat calls it a 'regular file' and 0 bytes long, and actually performs a read() syscall with a length of 0.

> 3) Furthermore, it might check whether the file is a regular file. If not, it is almost certainly not what the programmer had in mind.

gcc probably does this. (It would explain why /dev/stdin works)