r/C_Programming • u/Materac_YT • Sep 25 '26
POSIX exec
hey guys i am trying to write POSIX compatible program and i need to close the fds before i exec, i cant guarante they all fd's are O_CLOEXEC becouse i use libraries
0
Upvotes
View all comments
Show parent comment
1
u/flyingron Sep 25 '26 edited Sep 25 '26
Yes. I realized it after I posted it.
In fact, not having 0, 1, and 2 all open before exec can lead to some very interesting problems.
Years ago (probably around 1979 or so), I was working as a computer operator on an Early UNIX (Research V6) system at Johns Hopkins. Our mount program (which was setuid so all could run it with restriction) opened the superblock up on the device to be mounted (in user mode) so it could read some information prior to allowing the mount. Turned out one of our brighter guys decided to supress the messages mount output by closing FD 1. The message went out over the root inode of his disk. After spending some time fixing the corrupted filesystem by hand and finding out what had happened, I started hunting around for other setuid programs so afflicted.
I found a few but got tired and went home to sleep. The next morning I checked the paper log book we kept of system crashes and the message said that they had to restore the accounting file because the first eight characters were corrupted. "I think I know how that happened," said I. The other systems guys said, "We figured you did. It was your username that was written over those bytes."
Back when nofiles was only 16 or so, you could open 16 files and invoke su and it would fail to open /etc/passwd and just give you a root shell because it figured something seriously was wrong.