浏览代码

guard against hard links to non-ordinary-files when reading tcb shadow

Rich Felker 14 年之前
父节点
当前提交
976f42d1f1
共有 1 个文件被更改,包括 4 次插入2 次删除
  1. 4 2
      src/passwd/getspnam_r.c

+ 4 - 2
src/passwd/getspnam_r.c

@@ -1,5 +1,6 @@
 #include <fcntl.h>
 #include <unistd.h>
+#include <sys/stat.h>
 #include "pwf.h"
 
 /* This implementation support Openwall-style TCB passwords in place of
@@ -34,8 +35,9 @@ int getspnam_r(const char *name, struct spwd *sp, char *buf, size_t size, struct
 
 	fd = open(path, O_RDONLY|O_NOFOLLOW|O_NONBLOCK);
 	if (fd >= 0) {
-		f = fdopen(fd, "rb");
-		if (!f) {
+		struct stat st = { 0 };
+		errno = EINVAL;
+		if (fstat(fd, &st) || !S_ISREG(st.st_mode) || !(f = fdopen(fd, "rb"))) {
 			close(fd);
 			return errno;
 		}