shithub: riscv

Download patch

ref: 394af5a2162c44a4e2f16897b611f622f8dbbfac
parent: 6de804b578e54fb2e7d24e56c3032def4d24547e
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Feb 26 18:10:17 EST 2017

authsrv: handle short reads in initkeyseed()

--- a/sys/src/cmd/auth/authsrv.c
+++ b/sys/src/cmd/auth/authsrv.c
@@ -1008,14 +1008,20 @@
 	int fd;
 
 	genrandom(keyseed, sizeof(keyseed));
-	if((fd = create("/adm/keyseed", OWRITE|OEXCL, 0600)) >= 0){
-		write(fd, keyseed, sizeof(keyseed));
-	} else if((fd = open("/adm/keyseed", OREAD)) >= 0){
-		read(fd, keyseed, sizeof(keyseed));
-	} else{
-		syslog(0, AUTHLOG, "initkeyseed: no seed file: %r");
+	if((fd = open("/adm/keyseed", OREAD)) >= 0){
+		werrstr("file truncated");
+		if(read(fd, keyseed, sizeof(keyseed)) == sizeof(keyseed)){
+			close(fd);
+			return;
+		}
+		close(fd);
+	}
+	syslog(0, AUTHLOG, "initkeyseed: no keyseed: %r");
+	if((fd = create("/adm/keyseed", OWRITE, 0600)) < 0){
+		syslog(0, AUTHLOG, "initkeyseed: can't create: %r");
 		return;
 	}
+	write(fd, keyseed, sizeof(keyseed));
 	close(fd);
 }