![]() |
![]() |
|
![]() |
![]() |
|
Programmierung Rat & Tat für Programmierer |
![]() |
|
Themen-Optionen | Ansicht |
![]() |
#1 |
Jr. Member
![]() Registriert seit: 05.09.2001
Beiträge: 31
|
![]() Hallo,
ich habe das Problem folgenden unsicheren Code in einen sicheren Code umschreiben zu müssen. Dieses Programm erzeugt temporäre Dateien nach einem Schema, dass eine Race Condition, zwischen Erzeugung der temporären Datei und dem Nachsehen ob es eine Datei mit diesem Namen schon gibt, zulässt. Soweit ich weiss ist dies mit Verwendung von mkstemp(3) kein Problem mehr. Nur weiss ich nicht wie ich es umsetzen soll. Dem Programm übergibt man als Kommandozeilenargument ein Präfix, dass um die PID und eine fortlaufende Nummer erweitert, als Name der temorären Datei verwendet wird. #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <limits.h> /* times are in mikroseconds */ /* #define USLEEPTIME 5000 */ #define LOOPTIME 10000 void usage(const char *progname) { fprintf(stderr, "%s: usage\n" "%s <tempfile>", progname, progname); } int main(int argc, char *argv[]) { const char *tempfile; struct timespec nano; struct stat statbuf; char buf[80]; char filename[PATH_MAX + 100]; int fd; int rc; int cnt = 0; if (argc < 2) { usage(argv[0]); exit(1); } tempfile = argv[1]; sprintf(filename, "%s.%d.%d", tempfile, getpid(), cnt++); printf("using %s as temporary file\n", filename); sprintf(buf, "%ld\n", getpid()); for ( ; ; usleep(LOOPTIME) ) { /* check for existing tempfile ... */ if (stat(filename, &statbuf) == 0 || lstat(filename, &statbuf) == 0) { sprintf(filename, "%s.%d.%d", tempfile, getpid(), cnt++); printf("\ntempfile exists already - retrying with %s\n", filename); continue; } usleep(LOOPTIME); fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0600); if (fd == -1) { perror("open"); exit(2); } rc = write(fd, buf, strlen(buf)); if (rc == -1) { perror("write"); exit(2); } close(fd); unlink(filename); printf("."); fflush(stdout); } return 0; } Wie macht man daraus ein sicheres Programm? AlphaCentauri
____________________________________
AlphaCentauri |
![]() |
![]() |
![]() |
#2 |
Inventar
![]() Registriert seit: 24.09.2001
Beiträge: 7.335
|
![]() Code:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <limits.h> /* times are in mikroseconds */ /* #define USLEEPTIME 5000 */ #define LOOPTIME 10000 void usage(const char *progname) { fprintf(stderr, "%s: usage\n" "%s <tempfile>", progname, progname); } int main(int argc, char *argv[]) { const char *tempfile; struct timespec nano; struct stat statbuf; char buf[80]; char filename[PATH_MAX + 100]; int fd; int rc; int cnt = 0; if (argc < 2) { usage(argv[0]); exit(1); } tempfile = argv[1]; sprintf(filename, "%s.%d.%d.XXXXXX", tempfile, getpid(), cnt++); printf("using %s as temporary file\n", filename); sprintf(buf, "%ld\n", getpid()); for ( ; ; usleep(LOOPTIME) ) { /* check for existing tempfile ... */ if (stat(filename, &statbuf) == 0 || lstat(filename, &statbuf) == 0) { sprintf(filename, "%s.%d.%d", tempfile, getpid(), cnt++); printf("\ntempfile exists already - retrying with %s\n", filename); continue; } usleep(LOOPTIME); /* fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0600); */ memset(filename, '\0',PATH_MAX+100); sprintf(filename, "%s.%d.%d.XXXXXX", tempfile, getpid(), cnt++); printf("using %s as temporary file\n", filename); fd = mkstemp(filename); if (fd == -1) { perror("open"); exit(2); } printf("The REAL temporary filename is: >%s<\n", filename); rc = write(fd, buf, strlen(buf)); if (rc == -1) { perror("write"); exit(2); } close(fd); unlink(filename); printf("."); fflush(stdout); } return 0; }
____________________________________
Weiterhin zu finden auf http://martin.leyrer.priv.at , http://twitter.com/leyrer , http://www.debattierclub.net/ , http://www.tratschen.at/ und via Instant Messaging auf Jabber: m3 <ät> cargal.org . |
![]() |
![]() |
![]() |
Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1) | |
|
|