Noted by Jarno Huuskonen <Jarno.Huuskonen@uku.fi>.
int fd;
int flags;
FILE *f;
- struct stat sb;
+ struct stat sb, sb2;
char buf[2048];
/* Defend against symlink attacks */
flags = O_RDWR;
#endif
- if (stat (s, &sb) == 0)
+ if (lstat (s, &sb) == 0 && S_ISREG(sb.st_mode))
{
if ((fd = open (s, flags)) < 0)
return;
+
+ if ((fstat (fd, &sb2) != 0) || !S_ISREG (sb2.st_mode)
+ || (sb.st_dev != sb2.st_dev) || (sb.st_ino != sb2.st_ino))
+ {
+ close (fd);
+ return;
+ }
+
if ((f = fdopen (fd, "r+")))
{
unlink (s);