From: Glynn Clements <glynn.clements@virgin.net>
To: GOMEZ NOGUERA DAVIDEDUARDO <davidgn@servidor.unam.mx>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Misc C question
Date: Fri, 21 Jun 2002 00:24:03 +0100 [thread overview]
Message-ID: <aetruc$5h8$2@main.gmane.org> (raw)
In-Reply-To: <Pine.GSO.4.33.0206201540260.13436-100000@servidor.unam.mx>
GOMEZ NOGUERA DAVIDEDUARDO wrote:
> I read a while ago of some security issues on doing checks on files before
> opening, wich is harmfull if the program is suid.
>
> But i dont remember what to do about it.
> can someone refresh it to me?
I presume that you are referring to race conditions, which are a
consequence of point 3 of my previous message:
> 3. Functions which return information about the filesystem (e.g.
> stat/lstat) return information about the past. Just because the file
> existed (or did not exist) at the time of the check, there's no
> guarantee that the file still exists (or doesn't exist).
A race condition is a situation where the program's behaviour depends
upon the timing of certain events.
An example: suppose that a program wants to traverse a directory tree
(in the sense of the "find" command, "rm -r", etc). When traversing a
directory tree, you need to ignore symbolic links.
A simple approach might look something like this:
struct stat st;
...
if (lstat(filename, &st) < 0)
error();
if (S_ISDIR(st.st_mode))
{
chdir(filename);
...
}
The problem with this is that the directory could be renamed and
replaced with a symbolic link between the call to lstat() and the call
to chdir(), causing the program to chdir() to wherever the symlink
points.
Note that an attacker (someone who is trying to cause a process to
malfunction) may be able to force the lstat and/or chdir calls to take
a long time by using a combination of deeply nested directories,
directories containing a large number of entries, and long "chains" of
symbolic links. So, exploiting this type of race condition isn't as
difficult as it may seem.
One solution is to perform lstat(".", ...) after the chdir, to check
that you changed to the correct directory. This is likely to be safe,
as most systems won't let you rename "." or "..".
> is there anyway you can lock a file access and tell the kernel?
Unless the OS supports mandatory locking (which is rare), locking a
file with flock/lockf/fcntl don't automatically prevent other programs
from accessing the file; they only prevent conflicting locks from
being obtained.
Similarly, lock files only affect programs which check for the
existence of lock files.
> and what would happen if the program crashes without unlocking the
> file?
Kernel locks are released automatically when the program terminates.
This is the main reason for using kernel locks instead of lock files.
The main reasons for using lock files are:
1. They work for device files (/dev/*), and
2. Older NFS implementations don't support kernel locks.
--
Glynn Clements <glynn.clements@virgin.net>
next prev parent reply other threads:[~2002-06-20 23:24 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-06-20 2:46 Misc C question xmp
2002-06-20 3:10 ` mike
2002-06-20 4:44 ` xmp
2002-06-20 5:08 ` mike
2002-06-20 12:46 ` William N. Zanatta
2002-06-20 6:28 ` Oleg O. Ossovitskii
2002-06-20 8:05 ` Mohammed Khalid Ansari
2002-06-20 8:51 ` Re[2]: " Oleg O. Ossovitskii
2002-06-20 13:23 ` Glynn Clements
2002-06-20 20:42 ` GOMEZ NOGUERA DAVIDEDUARDO
2002-06-20 21:53 ` James Stevenson
2002-06-20 23:24 ` Glynn Clements
2002-06-21 0:07 ` Christopher Quinn
2002-06-20 23:24 ` Glynn Clements [this message]
2002-06-20 18:09 ` James Stevenson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='aetruc$5h8$2@main.gmane.org' \
--to=glynn.clements@virgin.net \
--cc=davidgn@servidor.unam.mx \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).