I received a bug report
that has me scratching my head. In my program, I test the
writability of a directory (which is the user's home directory by
default) before having readline
collect history and write it to a file in that directory at program exit.
Here is the problematic code:
if (access(dirname, W_OK) != 0)
complain("cannot create %s in %s", filename, dirname, errno);
else {
use_readline_for_stuff();
write_history("%s/%s", dirname, filename);
exit(0);
}
(This is pseudo-C, of course. The real code is here)
The user (let's call her USER
) reports getting an error message "cannot create xxx in
/home/USER: permission denied", that disappears after manually creating the file with USER@host > touch /home/USER/xxx
.
I would expect an unwritable home directory to lead to all kind of
problems, but the user can even simply touch
a file in that directory.
The program doesn't run suid, USER owns her home directory (and clearly can create files in it). My program is apparently the only program that displays this kind of problem. It has been fairly widely used for years, and this is the first time this bug gets reported
The Linux manpage for access (2)
says
if a directory is found to be writable, it probably means that files can be created in the directory
Why probably? Why (and when) not always (apart from a race condition like someone changing permissions just after the access()
call, full inode tables or hitting user limits) all of which don't seem to be the problem here - especially since the access()
call fails, and touch xxx
succeeds.