2 * Copyright (c) 1999 Sendmail, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
12 ** This program tests your system to see if you have the lovely
13 ** security-defeating semantics that an open with O_CREAT|O_EXCL
14 ** set will successfully open a file named by a symbolic link that
15 ** points to a non-existent file. Sadly, Posix is mute on what
16 ** should happen in this situation.
41 #include <sys/types.h>
49 static char id[] = "@(#)$Id: t_exclopen.c,v 8.6 2001/09/23 03:35:41 ca Exp $";
52 static char Attacker[128];
53 static char Attackee[128];
59 (void) unlink(Attacker);
60 (void) unlink(Attackee);
71 sprintf(Attacker, "/tmp/attacker.%d.%ld", getpid(), time(NULL));
72 sprintf(Attackee, "/tmp/attackee.%d.%ld", getpid(), time(NULL));
74 if (symlink(Attackee, Attacker) < 0)
76 printf("Could not create %s->%s symlink: %d\n",
77 Attacker, Attackee, errno);
80 (void) unlink(Attackee);
81 if (stat(Attackee, &st) >= 0)
83 printf("%s already exists -- remove and try again.\n",
87 if (open(Attacker, O_WRONLY|O_CREAT|O_EXCL, 0644) < 0)
89 int save_errno = errno;
91 if (stat(Attackee, &st) >= 0)
93 printf("Weird. Open failed but %s was created anyhow (errno = %d)\n",
94 Attackee, save_errno);
97 printf("Good show! Exclusive open works properly with symbolic links (errno = %d).\n",
101 if (stat(Attackee, &st) < 0)
103 printf("Weird. Open succeeded but %s was not created\n",
107 printf("Bad news: you can do an exclusive open through a symbolic link\n");
108 printf("\tBe sure you #define BOGUS_O_EXCL in conf.h\n");