7153347: System read/stat/open calls should be hardened to handle EINTR

Reviewed-by: okutsu
This commit is contained in:
Nishit Jain 2016-08-02 10:31:05 +09:00
parent ee3076d84b
commit a92810e865

View file

@ -44,6 +44,12 @@
#define SKIP_SPACE(p) while (*p == ' ' || *p == '\t') p++; #define SKIP_SPACE(p) while (*p == ' ' || *p == '\t') p++;
#define RESTARTABLE(_cmd, _result) do { \
do { \
_result = _cmd; \
} while((_result == -1) && (errno == EINTR)); \
} while(0)
#if defined(_ALLBSD_SOURCE) #if defined(_ALLBSD_SOURCE)
#define dirent64 dirent #define dirent64 dirent
#define readdir64_r readdir_r #define readdir64_r readdir_r
@ -121,6 +127,7 @@ findZoneinfoFile(char *buf, size_t size, const char *dir)
int fd = -1; int fd = -1;
char *dbuf = NULL; char *dbuf = NULL;
char *tz = NULL; char *tz = NULL;
int res;
dirp = opendir(dir); dirp = opendir(dir);
if (dirp == NULL) { if (dirp == NULL) {
@ -161,7 +168,8 @@ findZoneinfoFile(char *buf, size_t size, const char *dir)
if (pathname == NULL) { if (pathname == NULL) {
break; break;
} }
if (stat(pathname, &statbuf) == -1) { RESTARTABLE(stat(pathname, &statbuf), res);
if (res == -1) {
break; break;
} }
@ -175,10 +183,12 @@ findZoneinfoFile(char *buf, size_t size, const char *dir)
if (dbuf == NULL) { if (dbuf == NULL) {
break; break;
} }
if ((fd = open(pathname, O_RDONLY)) == -1) { RESTARTABLE(open(pathname, O_RDONLY), fd);
if (fd == -1) {
break; break;
} }
if (read(fd, dbuf, size) != (ssize_t) size) { RESTARTABLE(read(fd, dbuf, size), res);
if (res != (ssize_t) size) {
break; break;
} }
if (memcmp(buf, dbuf, size) == 0) { if (memcmp(buf, dbuf, size) == 0) {
@ -230,6 +240,7 @@ getPlatformTimeZoneID()
int fd; int fd;
char *buf; char *buf;
size_t size; size_t size;
int res;
#if defined(__linux__) #if defined(__linux__)
/* /*
@ -260,7 +271,8 @@ getPlatformTimeZoneID()
/* /*
* Next, try /etc/localtime to find the zone ID. * Next, try /etc/localtime to find the zone ID.
*/ */
if (lstat(DEFAULT_ZONEINFO_FILE, &statbuf) == -1) { RESTARTABLE(lstat(DEFAULT_ZONEINFO_FILE, &statbuf), res);
if (res == -1) {
return NULL; return NULL;
} }
@ -294,10 +306,13 @@ getPlatformTimeZoneID()
* If initial symbolic link resolution failed, we should treat target * If initial symbolic link resolution failed, we should treat target
* file as a regular file. * file as a regular file.
*/ */
if ((fd = open(DEFAULT_ZONEINFO_FILE, O_RDONLY)) == -1) { RESTARTABLE(open(DEFAULT_ZONEINFO_FILE, O_RDONLY), fd);
if (fd == -1) {
return NULL; return NULL;
} }
if (fstat(fd, &statbuf) == -1) {
RESTARTABLE(fstat(fd, &statbuf), res);
if (res == -1) {
(void) close(fd); (void) close(fd);
return NULL; return NULL;
} }
@ -308,7 +323,8 @@ getPlatformTimeZoneID()
return NULL; return NULL;
} }
if (read(fd, buf, size) != (ssize_t) size) { RESTARTABLE(read(fd, buf, size), res);
if (res != (ssize_t) size) {
(void) close(fd); (void) close(fd);
free((void *) buf); free((void *) buf);
return NULL; return NULL;
@ -372,7 +388,8 @@ fileopen(const char *fname, const char *fmode)
/* /*
* It assumes read open. * It assumes read open.
*/ */
if ((fd = open(fname, O_RDONLY)) == -1) { RESTARTABLE(open(fname, O_RDONLY), fd);
if (fd == -1) {
return NULL; return NULL;
} }
@ -420,7 +437,8 @@ filegets(char *s, int n, FILE *stream)
if (iop->ptr == iop->endptr) { if (iop->ptr == iop->endptr) {
ssize_t len; ssize_t len;
if ((len = read(iop->fd, (void *)iop->buffer, BUFFER_SIZE)) == -1) { RESTARTABLE(read(iop->fd, (void *)iop->buffer, BUFFER_SIZE), len);
if (len == -1) {
return NULL; return NULL;
} }
if (len == 0) { if (len == 0) {
@ -558,6 +576,7 @@ getSolarisDefaultZoneID() {
size_t size; size_t size;
char *buf; char *buf;
int fd; int fd;
int res;
/* scf specific variables */ /* scf specific variables */
scf_handle_t *h = NULL; scf_handle_t *h = NULL;
scf_snapshot_t *snap = NULL; scf_snapshot_t *snap = NULL;
@ -593,7 +612,8 @@ getSolarisDefaultZoneID() {
} }
cleanupScf(h, snap, inst, pg, prop, val, tz); cleanupScf(h, snap, inst, pg, prop, val, tz);
if (stat(DEFAULT_ZONEINFO_FILE, &statbuf) == -1) { RESTARTABLE(stat(DEFAULT_ZONEINFO_FILE, &statbuf), res);
if (res == -1) {
return NULL; return NULL;
} }
size = (size_t) statbuf.st_size; size = (size_t) statbuf.st_size;
@ -601,12 +621,14 @@ getSolarisDefaultZoneID() {
if (buf == NULL) { if (buf == NULL) {
return NULL; return NULL;
} }
if ((fd = open(DEFAULT_ZONEINFO_FILE, O_RDONLY)) == -1) { RESTARTABLE(open(DEFAULT_ZONEINFO_FILE, O_RDONLY), fd);
if (fd == -1) {
free((void *) buf); free((void *) buf);
return NULL; return NULL;
} }
if (read(fd, buf, size) != (ssize_t) size) { RESTARTABLE(read(fd, buf, size), res);
if (res != (ssize_t) size) {
(void) close(fd); (void) close(fd);
free((void *) buf); free((void *) buf);
return NULL; return NULL;