Merge "Fix deadlock between vold and init" am: ec58e5851d am: b957474b61 am: 3577e4c351
am: 4506b67f37

Change-Id: I539b878c4d7a1372a0245ca5af5d38a846f0f667
diff --git a/Android.mk b/Android.mk
index d9f8753..e5eb89d 100644
--- a/Android.mk
+++ b/Android.mk
@@ -37,8 +37,6 @@
 	external/scrypt/lib/crypto \
 	frameworks/native/include \
 	system/security/keystore \
-	hardware/libhardware/include/hardware \
-	system/security/softkeymaster/include/keymaster
 
 common_shared_libraries := \
 	libsysutils \
@@ -57,6 +55,7 @@
 	libhardware \
 	libsoftkeymaster \
 	libbase \
+	libsoftkeymasterdevice \
 	libkeymaster_messages \
 
 common_static_libraries := \
@@ -148,3 +147,5 @@
 LOCAL_CONLYFLAGS := $(vold_conlyflags)
 
 include $(BUILD_EXECUTABLE)
+
+include $(LOCAL_PATH)/tests/Android.mk
diff --git a/BenchmarkGen.h b/BenchmarkGen.h
index 8a4f68b..cd95aae 100644
--- a/BenchmarkGen.h
+++ b/BenchmarkGen.h
@@ -211,7 +211,7 @@
 TEMP_FAILURE_RETRY(pread(t3433f17, buf, 30, 32521955));
 TEMP_FAILURE_RETRY(pread(t3433f17, buf, 45, 32521985));
 TEMP_FAILURE_RETRY(pread(t3433f17, buf, 6350, 32518144)); // mmap2
-int  t3450f18 = TEMP_FAILURE_RETRY(open("file11", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+int  t3450f18 = TEMP_FAILURE_RETRY(open("file11", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 int  t3450f22 = TEMP_FAILURE_RETRY(open("file12", O_RDONLY|O_LARGEFILE));
 TEMP_FAILURE_RETRY(read(t3450f22, buf, 1));
 close(t3450f22);
@@ -219,7 +219,7 @@
 close(t3450f22);
 t3450f22 = TEMP_FAILURE_RETRY(open("file14", O_RDONLY|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC));
 close(t3450f22);
-t3450f22 = TEMP_FAILURE_RETRY(open("file15", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3450f22 = TEMP_FAILURE_RETRY(open("file15", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3450f22, buf, 1));
 TEMP_FAILURE_RETRY(fsync(t3450f22));
 close(t3450f22);
@@ -390,7 +390,7 @@
 TEMP_FAILURE_RETRY(pread(t3455f18, buf, 22960, 0)); // mmap2
 TEMP_FAILURE_RETRY(pread(t3455f18, buf, 4108, 20480)); // mmap2
 close(t3455f18);
-t3455f18 = TEMP_FAILURE_RETRY(open("file11", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3455f18 = TEMP_FAILURE_RETRY(open("file11", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 30, 3513185));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 46, 3513215));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 3262, 3510272)); // mmap2
@@ -404,7 +404,7 @@
 close(t3455f19);
 t3455f19 = TEMP_FAILURE_RETRY(open("file14", O_RDONLY|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC));
 close(t3455f19);
-t3455f19 = TEMP_FAILURE_RETRY(open("file15", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3455f19 = TEMP_FAILURE_RETRY(open("file15", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3455f19, buf, 1));
 TEMP_FAILURE_RETRY(fsync(t3455f19));
 close(t3455f19);
@@ -415,7 +415,7 @@
 TEMP_FAILURE_RETRY(pread(t3455f18, buf, 13748, 0)); // mmap2
 TEMP_FAILURE_RETRY(pread(t3455f18, buf, 4196, 12288)); // mmap2
 close(t3455f18);
-int  t3483f20 = TEMP_FAILURE_RETRY(open("file17", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3483f20 = TEMP_FAILURE_RETRY(open("file17", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3483f20, buf, 100, 0));
 int  t3483f21 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3483f21, buf, 1, 0));
@@ -467,7 +467,7 @@
 TEMP_FAILURE_RETRY(pread(t3483f20, buf, 4096, 49152));
 TEMP_FAILURE_RETRY(pread(t3483f20, buf, 4096, 139264));
 TEMP_FAILURE_RETRY(pread(t3483f20, buf, 4096, 172032));
-int  t3483f25 = TEMP_FAILURE_RETRY(open("file19", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3483f25 = TEMP_FAILURE_RETRY(open("file19", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3483f25, buf, 100, 0));
 int  t3483f26 = TEMP_FAILURE_RETRY(open("file20", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3483f26, buf, 1, 0));
@@ -550,7 +550,7 @@
 int  t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f30, buf, 1, 0));
 close(t3499f30);
-int  t3499f31 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3499f31 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 4096, 516));
@@ -574,7 +574,7 @@
 t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f30, buf, 1, 0));
 close(t3499f30);
-t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 4096, 516));
@@ -786,7 +786,7 @@
 t3499f31 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f31, buf, 1, 0));
 close(t3499f31);
-t3499f31 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f31 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 4096, 516));
@@ -813,7 +813,7 @@
 t3499f31 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f31, buf, 1, 0));
 close(t3499f31);
-t3499f31 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f31 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 4096, 516));
@@ -837,7 +837,7 @@
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 28, 0));
 TEMP_FAILURE_RETRY(fdatasync(t3499f31));
 close(t3499f31);
-int  t3492f31 = TEMP_FAILURE_RETRY(open("file30", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3492f31 = TEMP_FAILURE_RETRY(open("file30", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3492f31, buf, 100, 0));
 int  t3492f32 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3492f32, buf, 1, 0));
@@ -883,7 +883,7 @@
 t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f30, buf, 1, 0));
 close(t3499f30);
-t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 4096, 516));
@@ -910,7 +910,7 @@
 t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f30, buf, 1, 0));
 close(t3499f30);
-t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 4096, 516));
@@ -937,7 +937,7 @@
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 30, 36389306));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 23, 36389336));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 1664, 36388864)); // mmap2
-t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 4096, 516));
@@ -1049,7 +1049,7 @@
 int  t3499f41 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f41, buf, 1, 0));
 close(t3499f41);
-t3499f41 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f41 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f41, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f41, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f41, buf, 4096, 516));
@@ -1076,7 +1076,7 @@
 t3499f41 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f41, buf, 1, 0));
 close(t3499f41);
-t3499f41 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f41 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f41, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f41, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f41, buf, 4096, 516));
@@ -1111,7 +1111,7 @@
 close(t3492f40);
 TEMP_FAILURE_RETRY(pread(t3492f31, buf, 16, 24));
 TEMP_FAILURE_RETRY(pread(t3492f31, buf, 4096, 36864));
-t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4096, 516));
@@ -1138,7 +1138,7 @@
 t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f40, buf, 1, 0));
 close(t3499f40);
-t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4096, 516));
@@ -1165,7 +1165,7 @@
 t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f40, buf, 1, 0));
 close(t3499f40);
-t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4096, 516));
@@ -1210,7 +1210,7 @@
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 30, 35636928));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 60, 35636958));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 2062, 35635200)); // mmap2
-t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4096, 516));
@@ -1237,7 +1237,7 @@
 t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f40, buf, 1, 0));
 close(t3499f40);
-t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4096, 516));
@@ -1279,7 +1279,7 @@
 int  t3499f33 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f33, buf, 1, 0));
 close(t3499f33);
-t3499f33 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f33 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f33, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f33, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f33, buf, 4096, 516));
@@ -1306,7 +1306,7 @@
 t3499f33 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f33, buf, 1, 0));
 close(t3499f33);
-t3499f33 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f33 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f33, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f33, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f33, buf, 4096, 516));
@@ -1474,7 +1474,7 @@
 int  t3499f50 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f50, buf, 1, 0));
 close(t3499f50);
-t3499f50 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f50 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f50, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f50, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f50, buf, 4096, 516));
@@ -1501,7 +1501,7 @@
 t3499f50 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f50, buf, 1, 0));
 close(t3499f50);
-t3499f50 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f50 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f50, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f50, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f50, buf, 4096, 516));
@@ -1796,7 +1796,7 @@
 int  t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f55, buf, 1, 0));
 close(t3499f55);
-t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 4096, 516));
@@ -1834,8 +1834,8 @@
 TEMP_FAILURE_RETRY(read(t3532f55, buf, 16384));
 TEMP_FAILURE_RETRY(read(t3532f55, buf, 16384));
 close(t3532f55);
-t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
-int  t3505f56 = TEMP_FAILURE_RETRY(open("file59", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
+int  t3505f56 = TEMP_FAILURE_RETRY(open("file59", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 4096, 516));
@@ -1863,7 +1863,7 @@
 int  t3499f58 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f58, buf, 1, 0));
 close(t3499f58);
-t3499f58 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f58 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f58, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f58, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f58, buf, 4096, 516));
@@ -1896,7 +1896,7 @@
 t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f55, buf, 1, 0));
 close(t3499f55);
-t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 4096, 516));
@@ -1923,7 +1923,7 @@
 t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f55, buf, 1, 0));
 close(t3499f55);
-t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 4096, 516));
@@ -2012,7 +2012,7 @@
 t3505f56 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3505f56, buf, 1, 0));
 close(t3505f56);
-t3505f56 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3505f56 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3505f56, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3505f56, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3505f56, buf, 4096, 516));
@@ -2035,7 +2035,7 @@
 TEMP_FAILURE_RETRY(pwrite(t3505f56, buf, 4, 21032));
 TEMP_FAILURE_RETRY(pwrite(t3505f56, buf, 4096, 21036));
 TEMP_FAILURE_RETRY(pwrite(t3505f56, buf, 4, 25132));
-t3499f62 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f62 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f62, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f62, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f62, buf, 4096, 516));
@@ -2133,7 +2133,7 @@
 int  t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f66, buf, 1, 0));
 close(t3499f66);
-t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 4096, 516));
@@ -2196,7 +2196,7 @@
 TEMP_FAILURE_RETRY(read(t3532f61, buf, 16384));
 close(t3532f61);
 t3533f61 = TEMP_FAILURE_RETRY(open("file68", O_RDONLY|O_LARGEFILE));
-t3499f62 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f62 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f62, buf, 512, 0));
 TEMP_FAILURE_RETRY(read(t3533f61, buf, 16384));
 TEMP_FAILURE_RETRY(pwrite(t3499f62, buf, 4, 512));
@@ -2235,7 +2235,7 @@
 int  t3499f80 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f80, buf, 1, 0));
 close(t3499f80);
-t3499f80 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f80 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f80, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f80, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f80, buf, 4096, 516));
@@ -2259,7 +2259,7 @@
 t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f66, buf, 1, 0));
 close(t3499f66);
-t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 4096, 516));
@@ -2403,12 +2403,12 @@
 close(t3519f67);
 t3519f67 = TEMP_FAILURE_RETRY(open("file127", O_RDONLY|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC));
 close(t3519f67);
-t3519f70 = TEMP_FAILURE_RETRY(open("file128", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+t3519f70 = TEMP_FAILURE_RETRY(open("file128", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3519f70);
-int  t3526f70 = TEMP_FAILURE_RETRY(open("file129", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
-int  t3519f75 = TEMP_FAILURE_RETRY(open("file128", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+int  t3526f70 = TEMP_FAILURE_RETRY(open("file129", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
+int  t3519f75 = TEMP_FAILURE_RETRY(open("file128", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 close(t3526f70);
-t3526f70 = TEMP_FAILURE_RETRY(open("file129", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f70 = TEMP_FAILURE_RETRY(open("file129", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3519f75, buf, 2991));
 close(t3519f75);
 TEMP_FAILURE_RETRY(write(t3526f70, buf, 3974));
@@ -2419,7 +2419,7 @@
 TEMP_FAILURE_RETRY(pread(t3499f72, buf, 1, 0));
 close(t3499f72);
 TEMP_FAILURE_RETRY(read(t3519f67, buf, 16384));
-t3499f72 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f72 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 4096, 516));
@@ -2441,26 +2441,26 @@
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 28, 0));
 TEMP_FAILURE_RETRY(fdatasync(t3499f72));
 close(t3499f72);
-t3526f70 = TEMP_FAILURE_RETRY(open("file131", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+t3526f70 = TEMP_FAILURE_RETRY(open("file131", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f70);
-t3526f70 = TEMP_FAILURE_RETRY(open("file131", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f70 = TEMP_FAILURE_RETRY(open("file131", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f70, buf, 4622));
 close(t3526f70);
-int  t3526f72 = TEMP_FAILURE_RETRY(open("file132", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3526f72 = TEMP_FAILURE_RETRY(open("file132", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f72);
-t3526f72 = TEMP_FAILURE_RETRY(open("file132", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f72 = TEMP_FAILURE_RETRY(open("file132", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f72, buf, 16384));
 TEMP_FAILURE_RETRY(write(t3526f72, buf, 6849));
 close(t3526f72);
-t3526f70 = TEMP_FAILURE_RETRY(open("file133", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+t3526f70 = TEMP_FAILURE_RETRY(open("file133", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f70);
-int  t3526f75 = TEMP_FAILURE_RETRY(open("file133", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+int  t3526f75 = TEMP_FAILURE_RETRY(open("file133", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f75, buf, 13332));
 close(t3526f75);
 int  t3495f70 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3495f70, buf, 1, 0));
 close(t3495f70);
-t3495f70 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3495f70 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3495f70, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3495f70, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3495f70, buf, 4096, 516));
@@ -2481,9 +2481,9 @@
 TEMP_FAILURE_RETRY(pwrite(t3495f70, buf, 28, 0));
 TEMP_FAILURE_RETRY(fdatasync(t3495f70));
 close(t3495f70);
-int  t3526f93 = TEMP_FAILURE_RETRY(open("file134", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3526f93 = TEMP_FAILURE_RETRY(open("file134", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f93);
-int  t3526f88 = TEMP_FAILURE_RETRY(open("file134", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+int  t3526f88 = TEMP_FAILURE_RETRY(open("file134", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f88, buf, 15056));
 close(t3526f88);
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 30, 34433108));
@@ -2498,7 +2498,7 @@
 t3495f75 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3495f75, buf, 1, 0));
 close(t3495f75);
-t3495f75 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3495f75 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3495f75, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3495f75, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3495f75, buf, 4096, 516));
@@ -2522,7 +2522,7 @@
 int  t3499f74 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f74, buf, 1, 0));
 close(t3499f74);
-t3499f74 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f74 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f74, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f74, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f74, buf, 4096, 516));
@@ -2543,15 +2543,15 @@
 TEMP_FAILURE_RETRY(pwrite(t3499f74, buf, 28, 0));
 TEMP_FAILURE_RETRY(fdatasync(t3499f74));
 close(t3499f74);
-int  t3526f64 = TEMP_FAILURE_RETRY(open("file135", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3526f64 = TEMP_FAILURE_RETRY(open("file135", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f64);
-t3526f64 = TEMP_FAILURE_RETRY(open("file135", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f64 = TEMP_FAILURE_RETRY(open("file135", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f64, buf, 16384));
 TEMP_FAILURE_RETRY(write(t3526f64, buf, 4873));
 close(t3526f64);
-int  t3526f90 = TEMP_FAILURE_RETRY(open("file136", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3526f90 = TEMP_FAILURE_RETRY(open("file136", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f90);
-t3526f90 = TEMP_FAILURE_RETRY(open("file136", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f90 = TEMP_FAILURE_RETRY(open("file136", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f90, buf, 4199));
 close(t3526f90);
 int  t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
@@ -2560,7 +2560,7 @@
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 30, 35511105));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 28, 35511135));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 3217, 35508224)); // mmap2
-t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 30, 35511441));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 40, 35511471));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 512, 0));
@@ -2596,7 +2596,7 @@
 t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f90, buf, 1, 0));
 close(t3499f90);
-t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 4096, 516));
@@ -2623,7 +2623,7 @@
 t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f90, buf, 1, 0));
 close(t3499f90);
-t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 4096, 516));
@@ -2650,11 +2650,11 @@
 int  t3499f84 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f84, buf, 1, 0));
 close(t3499f84);
-t3499f72 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f72 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 4096, 516));
-int  t3495f84 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3495f84 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 4, 4612));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 4, 4616));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 4096, 4620));
@@ -2701,7 +2701,7 @@
 int  t3495f66 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3495f66, buf, 1, 0));
 close(t3495f66);
-t3495f66 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3495f66 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3495f66, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3495f66, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3495f66, buf, 4096, 516));
@@ -2725,7 +2725,7 @@
 t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f66, buf, 1, 0));
 close(t3499f66);
-t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 4096, 516));
@@ -2753,7 +2753,7 @@
 int  t3505f66 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3505f66, buf, 1, 0));
 close(t3505f66);
-t3505f66 = TEMP_FAILURE_RETRY(open("file138", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3505f66 = TEMP_FAILURE_RETRY(open("file138", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 t3526f70 = TEMP_FAILURE_RETRY(open("file139", O_RDONLY|O_LARGEFILE));
 TEMP_FAILURE_RETRY(read(t3526f70, buf, 16384));
 TEMP_FAILURE_RETRY(read(t3526f70, buf, 16384));
@@ -2764,7 +2764,7 @@
 t3495f70 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3495f70, buf, 1, 0));
 close(t3495f70);
-t3495f70 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3495f70 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3495f70, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3495f70, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3495f70, buf, 4096, 516));
@@ -2786,19 +2786,19 @@
 TEMP_FAILURE_RETRY(fdatasync(t3495f70));
 close(t3495f70);
 TEMP_FAILURE_RETRY(write(t3505f66, buf, 10592));
-int  t3533f70 = TEMP_FAILURE_RETRY(open("file140", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3533f70 = TEMP_FAILURE_RETRY(open("file140", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3533f70);
-t3533f70 = TEMP_FAILURE_RETRY(open("file140", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3533f70 = TEMP_FAILURE_RETRY(open("file140", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3533f70, buf, 4042));
 close(t3533f70);
 TEMP_FAILURE_RETRY(write(t3505f66, buf, 9664));
 TEMP_FAILURE_RETRY(write(t3505f66, buf, 23656));
-t3533f70 = TEMP_FAILURE_RETRY(open("file141", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+t3533f70 = TEMP_FAILURE_RETRY(open("file141", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3533f70);
-t3526f70 = TEMP_FAILURE_RETRY(open("file142", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
-int  t3533f72 = TEMP_FAILURE_RETRY(open("file141", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f70 = TEMP_FAILURE_RETRY(open("file142", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
+int  t3533f72 = TEMP_FAILURE_RETRY(open("file141", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 close(t3526f70);
-t3526f70 = TEMP_FAILURE_RETRY(open("file142", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f70 = TEMP_FAILURE_RETRY(open("file142", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f70, buf, 5057));
 TEMP_FAILURE_RETRY(write(t3533f72, buf, 4595));
 close(t3526f70);
@@ -2808,7 +2808,7 @@
 t3499f70 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f70, buf, 1, 0));
 close(t3499f70);
-t3499f70 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f70 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f70, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f70, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f70, buf, 4096, 516));
@@ -2836,7 +2836,7 @@
 t3505f66 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3505f66, buf, 1, 0));
 close(t3505f66);
-t3505f66 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3505f66 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3505f66, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3505f66, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3505f66, buf, 4096, 516));
@@ -2872,7 +2872,7 @@
 t3505f66 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3505f66, buf, 1, 0));
 close(t3505f66);
-t3505f66 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3505f66 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3505f66, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3505f66, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3505f66, buf, 4096, 516));
@@ -2923,9 +2923,9 @@
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 30, 34261624));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 49, 34261654));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 2993, 34258944)); // mmap2
-int  t3533f91 = TEMP_FAILURE_RETRY(open("file143", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3533f91 = TEMP_FAILURE_RETRY(open("file143", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3533f91);
-t3533f91 = TEMP_FAILURE_RETRY(open("file143", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3533f91 = TEMP_FAILURE_RETRY(open("file143", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 30, 31210525));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 44, 31210555));
 TEMP_FAILURE_RETRY(write(t3533f91, buf, 16384));
@@ -2935,7 +2935,7 @@
 int  t3499f91 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f91, buf, 1, 0));
 close(t3499f91);
-t3499f91 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f91 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f91, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f91, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f91, buf, 4096, 516));
@@ -2959,7 +2959,7 @@
 int  t3505f84 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3505f84, buf, 1, 0));
 close(t3505f84);
-t3505f84 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3505f84 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3505f84, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3505f84, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3505f84, buf, 4096, 516));
@@ -2980,41 +2980,41 @@
 TEMP_FAILURE_RETRY(pwrite(t3505f84, buf, 28, 0));
 TEMP_FAILURE_RETRY(fdatasync(t3505f84));
 close(t3505f84);
-int  t3533f102 = TEMP_FAILURE_RETRY(open("file144", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3533f102 = TEMP_FAILURE_RETRY(open("file144", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3533f102);
-t3533f72 = TEMP_FAILURE_RETRY(open("file144", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3533f72 = TEMP_FAILURE_RETRY(open("file144", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3533f72, buf, 5550));
 close(t3533f72);
-int  t3526f84 = TEMP_FAILURE_RETRY(open("file145", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3526f84 = TEMP_FAILURE_RETRY(open("file145", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f84);
-int  t3526f101 = TEMP_FAILURE_RETRY(open("file145", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+int  t3526f101 = TEMP_FAILURE_RETRY(open("file145", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f101, buf, 3612));
 close(t3526f101);
-t3526f90 = TEMP_FAILURE_RETRY(open("file146", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+t3526f90 = TEMP_FAILURE_RETRY(open("file146", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f90);
-t3526f90 = TEMP_FAILURE_RETRY(open("file146", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f90 = TEMP_FAILURE_RETRY(open("file146", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f90, buf, 5414));
-int  t3533f96 = TEMP_FAILURE_RETRY(open("file147", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3533f96 = TEMP_FAILURE_RETRY(open("file147", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3533f96);
-t3533f96 = TEMP_FAILURE_RETRY(open("file147", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3533f96 = TEMP_FAILURE_RETRY(open("file147", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 close(t3526f90);
 TEMP_FAILURE_RETRY(write(t3533f96, buf, 3834));
 close(t3533f96);
-int  t3519f90 = TEMP_FAILURE_RETRY(open("file148", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3519f90 = TEMP_FAILURE_RETRY(open("file148", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3519f90);
-t3519f90 = TEMP_FAILURE_RETRY(open("file148", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3519f90 = TEMP_FAILURE_RETRY(open("file148", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3519f90, buf, 3461));
-int  t3526f96 = TEMP_FAILURE_RETRY(open("file149", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3526f96 = TEMP_FAILURE_RETRY(open("file149", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f96);
 close(t3519f90);
-t3526f90 = TEMP_FAILURE_RETRY(open("file149", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f90 = TEMP_FAILURE_RETRY(open("file149", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f90, buf, 16384));
 TEMP_FAILURE_RETRY(write(t3526f90, buf, 12766));
 close(t3526f90);
-int  t3533f90 = TEMP_FAILURE_RETRY(open("file150", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3533f90 = TEMP_FAILURE_RETRY(open("file150", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3533f90);
-t3533f90 = TEMP_FAILURE_RETRY(open("file150", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
-int  t3505f96 = TEMP_FAILURE_RETRY(open("file151", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3533f90 = TEMP_FAILURE_RETRY(open("file150", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
+int  t3505f96 = TEMP_FAILURE_RETRY(open("file151", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3505f96, buf, 100, 0));
 TEMP_FAILURE_RETRY(write(t3533f90, buf, 10056));
 close(t3533f90);
@@ -3036,11 +3036,11 @@
 close(t3505f90);
 TEMP_FAILURE_RETRY(pread(t3505f96, buf, 16, 24));
 t3505f90 = TEMP_FAILURE_RETRY(open("file152", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
-int  t3533f99 = TEMP_FAILURE_RETRY(open("file153", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3533f99 = TEMP_FAILURE_RETRY(open("file153", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3533f99);
 TEMP_FAILURE_RETRY(pread(t3505f90, buf, 1, 0));
 close(t3505f90);
-t3533f90 = TEMP_FAILURE_RETRY(open("file153", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3533f90 = TEMP_FAILURE_RETRY(open("file153", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(pread(t3505f96, buf, 16, 24));
 TEMP_FAILURE_RETRY(write(t3533f90, buf, 13271));
 close(t3533f90);
@@ -3066,7 +3066,7 @@
 t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f90, buf, 1, 0));
 close(t3499f90);
-t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 4096, 516));
@@ -3090,7 +3090,7 @@
 t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f90, buf, 1, 0));
 close(t3499f90);
-t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 4096, 516));
@@ -3120,7 +3120,7 @@
 t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f92, buf, 1, 0));
 close(t3499f92);
-t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 4096, 516));
@@ -3147,7 +3147,7 @@
 t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f92, buf, 1, 0));
 close(t3499f92);
-t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 4096, 516));
@@ -3177,7 +3177,7 @@
 int  t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f100, buf, 1, 0));
 close(t3499f100);
-t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 4096, 516));
@@ -3201,7 +3201,7 @@
 t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f100, buf, 1, 0));
 close(t3499f100);
-t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 4096, 516));
@@ -3228,7 +3228,7 @@
 t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f100, buf, 1, 0));
 close(t3499f100);
-t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 4096, 516));
@@ -3252,7 +3252,7 @@
 t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f100, buf, 1, 0));
 close(t3499f100);
-t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 4096, 516));
@@ -3282,7 +3282,7 @@
 t3499f84 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f84, buf, 1, 0));
 close(t3499f84);
-int  t3499f27 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3499f27 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f27, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f27, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f27, buf, 4096, 516));
@@ -3309,7 +3309,7 @@
 t3499f27 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f27, buf, 1, 0));
 close(t3499f27);
-t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 4096, 516));
@@ -3336,7 +3336,7 @@
 t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f92, buf, 1, 0));
 close(t3499f92);
-t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 4096, 516));
@@ -3360,7 +3360,7 @@
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 28, 0));
 TEMP_FAILURE_RETRY(fdatasync(t3499f92));
 close(t3499f92);
-int  t3545f92 = TEMP_FAILURE_RETRY(open("file154", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3545f92 = TEMP_FAILURE_RETRY(open("file154", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3545f92, buf, 100, 0));
 int  t3545f97 = TEMP_FAILURE_RETRY(open("file155", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3545f97, buf, 1, 0));
@@ -3402,7 +3402,7 @@
 TEMP_FAILURE_RETRY(pread(t3545f97, buf, 1, 0));
 close(t3545f97);
 TEMP_FAILURE_RETRY(pread(t3545f92, buf, 16, 24));
-t3545f97 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3545f97 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3545f97, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3545f97, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3545f97, buf, 4096, 516));
@@ -3435,7 +3435,7 @@
 TEMP_FAILURE_RETRY(pread(t3545f29, buf, 1, 0));
 close(t3545f29);
 TEMP_FAILURE_RETRY(pread(t3545f92, buf, 16, 24));
-t3545f97 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3545f97 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3545f97, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3545f97, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3545f97, buf, 4096, 516));
@@ -3460,7 +3460,7 @@
 TEMP_FAILURE_RETRY(pwrite(t3545f97, buf, 28, 0));
 TEMP_FAILURE_RETRY(fdatasync(t3545f97));
 close(t3545f97);
-int  t3575f29 = TEMP_FAILURE_RETRY(open("file16", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3575f29 = TEMP_FAILURE_RETRY(open("file16", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0));
 TEMP_FAILURE_RETRY(write(t3575f29, buf, 17344));
 close(t3575f29);
 t3545f97 = TEMP_FAILURE_RETRY(open("file155", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
@@ -3471,7 +3471,7 @@
 TEMP_FAILURE_RETRY(pread(t3545f29, buf, 1, 0));
 close(t3545f29);
 TEMP_FAILURE_RETRY(pread(t3545f92, buf, 16, 24));
-t3545f29 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3545f29 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3545f29, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3545f29, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3545f29, buf, 4096, 516));
@@ -3499,7 +3499,7 @@
 int  t3499f26 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f26, buf, 1, 0));
 close(t3499f26);
-t3499f26 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f26 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f26, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f26, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f26, buf, 4096, 516));
@@ -3523,7 +3523,7 @@
 t3499f26 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f26, buf, 1, 0));
 close(t3499f26);
-int  t3499f28 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3499f28 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f28, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f28, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f28, buf, 4096, 516));
@@ -3569,7 +3569,7 @@
 TEMP_FAILURE_RETRY(pread(t3545f29, buf, 1, 0));
 close(t3545f29);
 TEMP_FAILURE_RETRY(pread(t3545f92, buf, 16, 24));
-t3545f29 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3545f29 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3545f29, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3545f29, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3545f29, buf, 4096, 516));
@@ -3598,7 +3598,7 @@
 TEMP_FAILURE_RETRY(pread(t3545f84, buf, 1, 0));
 close(t3545f84);
 TEMP_FAILURE_RETRY(pread(t3545f92, buf, 16, 24));
-int  t3584f84 = TEMP_FAILURE_RETRY(open("file157", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3584f84 = TEMP_FAILURE_RETRY(open("file157", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3584f84, buf, 100, 0));
 int  t3584f90 = TEMP_FAILURE_RETRY(open("file158", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3584f90, buf, 1, 0));
@@ -3608,7 +3608,7 @@
 TEMP_FAILURE_RETRY(pread(t3545f90, buf, 1, 0));
 close(t3545f90);
 TEMP_FAILURE_RETRY(pread(t3545f92, buf, 16, 24));
-t3545f90 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3545f90 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3545f90, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3545f90, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3545f90, buf, 4096, 516));
@@ -3657,7 +3657,7 @@
 TEMP_FAILURE_RETRY(pread(t3584f84, buf, 4096, 16384));
 TEMP_FAILURE_RETRY(pread(t3584f84, buf, 4096, 12288));
 TEMP_FAILURE_RETRY(pread(t3545f92, buf, 4096, 32768));
-t3545f99 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3545f99 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3545f99, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3545f99, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3545f99, buf, 4096, 516));
@@ -3785,8 +3785,8 @@
 t3499f28 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f28, buf, 1, 0));
 close(t3499f28);
-int  t3496f28 = TEMP_FAILURE_RETRY(open("file169", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
-int  t3499f103 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3496f28 = TEMP_FAILURE_RETRY(open("file169", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
+int  t3499f103 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f103, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f103, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f103, buf, 4096, 516));
@@ -3814,7 +3814,7 @@
 TEMP_FAILURE_RETRY(pread(t3499f106, buf, 1, 0));
 close(t3499f106);
 close(t3496f28);
-t3499f28 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f28 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f28, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f28, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f28, buf, 4096, 516));
@@ -3835,7 +3835,7 @@
 TEMP_FAILURE_RETRY(pwrite(t3499f28, buf, 28, 0));
 TEMP_FAILURE_RETRY(fdatasync(t3499f28));
 close(t3499f28);
-int  t3541f103 = TEMP_FAILURE_RETRY(open("file170", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3541f103 = TEMP_FAILURE_RETRY(open("file170", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3541f103, buf, 100, 0));
 int  t3541f105 = TEMP_FAILURE_RETRY(open("file171", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3541f105, buf, 1, 0));
@@ -3872,7 +3872,7 @@
 int  t3499f107 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f107, buf, 1, 0));
 close(t3499f107);
-t3499f107 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f107 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f107, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f107, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f107, buf, 4096, 516));
@@ -3896,7 +3896,7 @@
 t3499f107 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f107, buf, 1, 0));
 close(t3499f107);
-t3499f105 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f105 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f105, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f105, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f105, buf, 4096, 516));
@@ -3920,7 +3920,7 @@
 int  t3505f106 = TEMP_FAILURE_RETRY(open("file171", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3505f106, buf, 1, 0));
 close(t3505f106);
-int  t3540f107 = TEMP_FAILURE_RETRY(open("file173", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3540f107 = TEMP_FAILURE_RETRY(open("file173", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3540f107, buf, 100, 0));
 int  t3540f108 = TEMP_FAILURE_RETRY(open("file174", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3540f108, buf, 1, 0));
@@ -3968,7 +3968,7 @@
 t3499f108 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f108, buf, 1, 0));
 close(t3499f108);
-int  t3597f108 = TEMP_FAILURE_RETRY(open("file177", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3597f108 = TEMP_FAILURE_RETRY(open("file177", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3597f108, buf, 100, 0));
 int  t3597f109 = TEMP_FAILURE_RETRY(open("file178", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3597f109, buf, 1, 0));
@@ -4035,7 +4035,7 @@
         LOG(ERROR) << "Failed to read random data";
         return -EIO;
     }
-    if ((out = TEMP_FAILURE_RETRY(open(name, O_WRONLY|O_CREAT|O_TRUNC))) < 0) {
+    if ((out = TEMP_FAILURE_RETRY(open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644))) < 0) {
         PLOG(ERROR) << "Failed to open " << name;
         return -errno;
     }
@@ -4436,3 +4436,4 @@
 
 }  // namespace vold
 }  // namespace android
+
diff --git a/CommandListener.cpp b/CommandListener.cpp
index b548a91..a312af2 100644
--- a/CommandListener.cpp
+++ b/CommandListener.cpp
@@ -703,8 +703,9 @@
             PLOG(ERROR) << "Failed to stat /proc/" << pid;
             return -errno;
         }
-        if (sb.st_uid != uid) {
-            LOG(ERROR) << "Mismatch UID expected=" << uid << ", actual=" << sb.st_uid;
+        if (sb.st_uid != AID_SYSTEM) {
+            LOG(ERROR) << "Only system can mount appfuse. UID expected=" << AID_SYSTEM
+                    << ", actual=" << sb.st_uid;
             return -EPERM;
         }
     }
diff --git a/Ext4Crypt.cpp b/Ext4Crypt.cpp
index 0063bef..682b34c 100644
--- a/Ext4Crypt.cpp
+++ b/Ext4Crypt.cpp
@@ -20,11 +20,14 @@
 #include "Utils.h"
 
 #include <algorithm>
+#include <chrono>
 #include <iomanip>
 #include <map>
+#include <mutex>
 #include <set>
 #include <sstream>
 #include <string>
+#include <thread>
 
 #include <dirent.h>
 #include <errno.h>
@@ -60,6 +63,8 @@
 static constexpr int FLAG_STORAGE_CE = 1 << 1;
 
 namespace {
+const std::chrono::seconds s_key_eviction_sleep_time(20);
+
 const std::string device_key_dir = std::string() + DATA_MNT_POINT + e4crypt_unencrypted_folder;
 const std::string device_key_path = device_key_dir + "/key";
 const std::string device_key_temp = device_key_dir + "/temp";
@@ -72,6 +77,10 @@
 // Some users are ephemeral, don't try to wipe their keys from disk
 std::set<userid_t> s_ephemeral_users;
 
+// Allow evictions to be cancelled.
+std::map<std::string, std::thread::id> s_desired_eviction_threads;
+std::mutex s_desired_eviction_threads_mutex;
+
 // Map user ids to key references
 std::map<userid_t, std::string> s_de_key_raw_refs;
 std::map<userid_t, std::string> s_ce_key_raw_refs;
@@ -158,6 +167,9 @@
     ext4_encryption_key ext4_key;
     if (!fill_key(key, &ext4_key)) return false;
     *raw_ref = generate_key_ref(ext4_key.raw, ext4_key.size);
+    // Ensure that no thread is waiting to evict this ref
+    std::lock_guard<std::mutex> lock(s_desired_eviction_threads_mutex);
+    s_desired_eviction_threads.erase(*raw_ref);
     auto ref = keyname(*raw_ref);
     key_serial_t device_keyring;
     if (!e4crypt_keyring(&device_keyring)) return false;
@@ -516,15 +528,66 @@
     return true;
 }
 
+static void evict_key_after_delay(const std::string raw_ref) {
+    LOG(DEBUG) << "Waiting to evict key in thread " << std::this_thread::get_id();
+    std::this_thread::sleep_for(s_key_eviction_sleep_time);
+    LOG(DEBUG) << "Done waiting to evict key in thread " << std::this_thread::get_id();
+
+    std::lock_guard<std::mutex> lock(s_desired_eviction_threads_mutex);
+    // Check the key should be evicted by this thread
+    auto search = s_desired_eviction_threads.find(raw_ref);
+    if (search == s_desired_eviction_threads.end()) {
+        LOG(DEBUG) << "Not evicting renewed-desirability key";
+        return;
+    }
+    if (search->second != std::this_thread::get_id()) {
+        LOG(DEBUG) << "We are not the thread to evict this key";
+        return;
+    }
+
+    // Remove the key from the keyring
+    auto ref = keyname(raw_ref);
+    key_serial_t device_keyring;
+    if (!e4crypt_keyring(&device_keyring)) return;
+    auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0);
+    if (keyctl_revoke(key_serial) != 0) {
+        PLOG(ERROR) << "Failed to revoke key with serial " << key_serial;
+        return;
+    }
+    LOG(DEBUG) << "Revoked key with serial " << key_serial;
+}
+
+static bool evict_key(const std::string &raw_ref) {
+    // FIXME the use of a thread with delay is a work around for a kernel bug
+    std::lock_guard<std::mutex> lock(s_desired_eviction_threads_mutex);
+    std::thread t(evict_key_after_delay, raw_ref);
+    s_desired_eviction_threads[raw_ref] = t.get_id();
+    LOG(DEBUG) << "Scheduled key eviction in thread " << t.get_id();
+    t.detach();
+    return true; // Sadly no way to know if we were successful :(
+}
+
+static bool evict_ce_key(userid_t user_id) {
+    s_ce_keys.erase(user_id);
+    bool success = true;
+    std::string raw_ref;
+    // If we haven't loaded the CE key, no need to evict it.
+    if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
+        success &= evict_key(raw_ref);
+    }
+    s_ce_key_raw_refs.erase(user_id);
+    return success;
+}
+
 bool e4crypt_destroy_user_key(userid_t user_id) {
     LOG(DEBUG) << "e4crypt_destroy_user_key(" << user_id << ")";
     if (!e4crypt_is_native()) {
         return true;
     }
     bool success = true;
-    s_ce_keys.erase(user_id);
     std::string raw_ref;
-    s_ce_key_raw_refs.erase(user_id);
+    success &= evict_ce_key(user_id);
+    success &= lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref) && evict_key(raw_ref);
     s_de_key_raw_refs.erase(user_id);
     auto it = s_ephemeral_users.find(user_id);
     if (it != s_ephemeral_users.end()) {
@@ -659,8 +722,9 @@
 
 // TODO: rename to 'evict' for consistency
 bool e4crypt_lock_user_key(userid_t user_id) {
+    LOG(DEBUG) << "e4crypt_lock_user_key " << user_id;
     if (e4crypt_is_native()) {
-        // TODO: remove from kernel keyring
+        return evict_ce_key(user_id);
     } else if (e4crypt_is_emulated()) {
         // When in emulation mode, we just use chmod
         if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
diff --git a/KeyStorage.cpp b/KeyStorage.cpp
index 986f403..e6c91f5 100644
--- a/KeyStorage.cpp
+++ b/KeyStorage.cpp
@@ -23,6 +23,7 @@
 #include <vector>
 
 #include <errno.h>
+#include <stdio.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/wait.h>
@@ -66,6 +67,7 @@
 static const std::string kStretchPrefix_scrypt = "scrypt ";
 static const char* kFn_encrypted_key = "encrypted_key";
 static const char* kFn_keymaster_key_blob = "keymaster_key_blob";
+static const char* kFn_keymaster_key_blob_upgraded = "keymaster_key_blob_upgraded";
 static const char* kFn_salt = "salt";
 static const char* kFn_secdiscardable = "secdiscardable";
 static const char* kFn_stretching = "stretching";
@@ -122,8 +124,8 @@
     return keymaster.generateKey(paramBuilder.build(), key);
 }
 
-static keymaster::AuthorizationSetBuilder beginParams(const KeyAuthentication& auth,
-                                                      const std::string& appId) {
+static keymaster::AuthorizationSet beginParams(const KeyAuthentication& auth,
+                                               const std::string& appId) {
     auto paramBuilder = keymaster::AuthorizationSetBuilder()
                             .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
                             .Authorization(keymaster::TAG_MAC_LENGTH, GCM_MAC_BYTES * 8)
@@ -133,45 +135,7 @@
         LOG(DEBUG) << "Supplying auth token to Keymaster";
         addStringParam(&paramBuilder, keymaster::TAG_AUTH_TOKEN, auth.token);
     }
-    return paramBuilder;
-}
-
-static bool encryptWithKeymasterKey(Keymaster& keymaster, const std::string& key,
-                                    const KeyAuthentication& auth, const std::string& appId,
-                                    const std::string& message, std::string* ciphertext) {
-    auto params = beginParams(auth, appId).build();
-    keymaster::AuthorizationSet outParams;
-    auto opHandle = keymaster.begin(KM_PURPOSE_ENCRYPT, key, params, &outParams);
-    if (!opHandle) return false;
-    keymaster_blob_t nonceBlob;
-    if (!outParams.GetTagValue(keymaster::TAG_NONCE, &nonceBlob)) {
-        LOG(ERROR) << "GCM encryption but no nonce generated";
-        return false;
-    }
-    // nonceBlob here is just a pointer into existing data, must not be freed
-    std::string nonce(reinterpret_cast<const char*>(nonceBlob.data), nonceBlob.data_length);
-    if (!checkSize("nonce", nonce.size(), GCM_NONCE_BYTES)) return false;
-    std::string body;
-    if (!opHandle.updateCompletely(message, &body)) return false;
-
-    std::string mac;
-    if (!opHandle.finishWithOutput(&mac)) return false;
-    if (!checkSize("mac", mac.size(), GCM_MAC_BYTES)) return false;
-    *ciphertext = nonce + body + mac;
-    return true;
-}
-
-static bool decryptWithKeymasterKey(Keymaster& keymaster, const std::string& key,
-                                    const KeyAuthentication& auth, const std::string& appId,
-                                    const std::string& ciphertext, std::string* message) {
-    auto nonce = ciphertext.substr(0, GCM_NONCE_BYTES);
-    auto bodyAndMac = ciphertext.substr(GCM_NONCE_BYTES);
-    auto params = addStringParam(beginParams(auth, appId), keymaster::TAG_NONCE, nonce).build();
-    auto opHandle = keymaster.begin(KM_PURPOSE_DECRYPT, key, params);
-    if (!opHandle) return false;
-    if (!opHandle.updateCompletely(bodyAndMac, message)) return false;
-    if (!opHandle.finish()) return false;
-    return true;
+    return paramBuilder.build();
 }
 
 static bool readFileToString(const std::string& filename, std::string* result) {
@@ -190,6 +154,79 @@
     return true;
 }
 
+static KeymasterOperation begin(Keymaster& keymaster, const std::string& dir,
+                                keymaster_purpose_t purpose,
+                                const keymaster::AuthorizationSet &keyParams,
+                                const keymaster::AuthorizationSet &opParams,
+                                keymaster::AuthorizationSet* outParams) {
+    auto kmKeyPath = dir + "/" + kFn_keymaster_key_blob;
+    std::string kmKey;
+    if (!readFileToString(kmKeyPath, &kmKey)) return KeymasterOperation();
+    keymaster::AuthorizationSet inParams;
+    inParams.push_back(keyParams);
+    inParams.push_back(opParams);
+    for (;;) {
+        auto opHandle = keymaster.begin(purpose, kmKey, inParams, outParams);
+        if (opHandle) {
+            return opHandle;
+        }
+        if (opHandle.error() != KM_ERROR_KEY_REQUIRES_UPGRADE) return opHandle;
+        LOG(DEBUG) << "Upgrading key: " << dir;
+        std::string newKey;
+        if (!keymaster.upgradeKey(kmKey, keyParams, &newKey)) return KeymasterOperation();
+        auto newKeyPath = dir + "/" + kFn_keymaster_key_blob_upgraded;
+        if (!writeStringToFile(newKey, newKeyPath)) return KeymasterOperation();
+        if (rename(newKeyPath.c_str(), kmKeyPath.c_str()) != 0) {
+            PLOG(ERROR) << "Unable to move upgraded key to location: " << kmKeyPath;
+            return KeymasterOperation();
+        }
+        if (!keymaster.deleteKey(kmKey)) {
+            LOG(ERROR) << "Key deletion failed during upgrade, continuing anyway: " << dir;
+        }
+        kmKey = newKey;
+        LOG(INFO) << "Key upgraded: " << dir;
+    }
+}
+
+static bool encryptWithKeymasterKey(Keymaster& keymaster, const std::string& dir,
+                                    const keymaster::AuthorizationSet &keyParams,
+                                    const std::string& message, std::string* ciphertext) {
+    keymaster::AuthorizationSet opParams;
+    keymaster::AuthorizationSet outParams;
+    auto opHandle = begin(keymaster, dir, KM_PURPOSE_ENCRYPT, keyParams, opParams, &outParams);
+    if (!opHandle) return false;
+    keymaster_blob_t nonceBlob;
+    if (!outParams.GetTagValue(keymaster::TAG_NONCE, &nonceBlob)) {
+        LOG(ERROR) << "GCM encryption but no nonce generated";
+        return false;
+    }
+    // nonceBlob here is just a pointer into existing data, must not be freed
+    std::string nonce(reinterpret_cast<const char*>(nonceBlob.data), nonceBlob.data_length);
+    if (!checkSize("nonce", nonce.size(), GCM_NONCE_BYTES)) return false;
+    std::string body;
+    if (!opHandle.updateCompletely(message, &body)) return false;
+
+    std::string mac;
+    if (!opHandle.finish(&mac)) return false;
+    if (!checkSize("mac", mac.size(), GCM_MAC_BYTES)) return false;
+    *ciphertext = nonce + body + mac;
+    return true;
+}
+
+static bool decryptWithKeymasterKey(Keymaster& keymaster, const std::string& dir,
+                                    const keymaster::AuthorizationSet &keyParams,
+                                    const std::string& ciphertext, std::string* message) {
+    auto nonce = ciphertext.substr(0, GCM_NONCE_BYTES);
+    auto bodyAndMac = ciphertext.substr(GCM_NONCE_BYTES);
+    auto opParams = addStringParam(keymaster::AuthorizationSetBuilder(),
+                                   keymaster::TAG_NONCE, nonce).build();
+    auto opHandle = begin(keymaster, dir, KM_PURPOSE_DECRYPT, keyParams, opParams, nullptr);
+    if (!opHandle) return false;
+    if (!opHandle.updateCompletely(bodyAndMac, message)) return false;
+    if (!opHandle.finish(nullptr)) return false;
+    return true;
+}
+
 static std::string getStretching() {
     char paramstr[PROPERTY_VALUE_MAX];
 
@@ -273,8 +310,9 @@
     std::string kmKey;
     if (!generateKeymasterKey(keymaster, auth, appId, &kmKey)) return false;
     if (!writeStringToFile(kmKey, dir + "/" + kFn_keymaster_key_blob)) return false;
+    auto keyParams = beginParams(auth, appId);
     std::string encryptedKey;
-    if (!encryptWithKeymasterKey(keymaster, kmKey, auth, appId, key, &encryptedKey)) return false;
+    if (!encryptWithKeymasterKey(keymaster, dir, keyParams, key, &encryptedKey)) return false;
     if (!writeStringToFile(encryptedKey, dir + "/" + kFn_encrypted_key)) return false;
     return true;
 }
@@ -296,13 +334,12 @@
     }
     std::string appId;
     if (!generateAppId(auth, stretching, salt, secdiscardable, &appId)) return false;
-    std::string kmKey;
-    if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, &kmKey)) return false;
     std::string encryptedMessage;
     if (!readFileToString(dir + "/" + kFn_encrypted_key, &encryptedMessage)) return false;
     Keymaster keymaster;
     if (!keymaster) return false;
-    return decryptWithKeymasterKey(keymaster, kmKey, auth, appId, encryptedMessage, key);
+    auto keyParams = beginParams(auth, appId);
+    return decryptWithKeymasterKey(keymaster, dir, keyParams, encryptedMessage, key);
 }
 
 static bool deleteKey(const std::string& dir) {
diff --git a/Keymaster.cpp b/Keymaster.cpp
index d271b6a..722d9f2 100644
--- a/Keymaster.cpp
+++ b/Keymaster.cpp
@@ -20,6 +20,7 @@
 #include <hardware/hardware.h>
 #include <hardware/keymaster1.h>
 #include <hardware/keymaster2.h>
+#include <keymaster/keymaster_configuration.h>
 
 namespace android {
 namespace vold {
@@ -31,6 +32,9 @@
     virtual keymaster_error_t generate_key(const keymaster_key_param_set_t* params,
                                            keymaster_key_blob_t* key_blob) const = 0;
     virtual keymaster_error_t delete_key(const keymaster_key_blob_t* key) const = 0;
+    virtual keymaster_error_t upgrade_key(const keymaster_key_blob_t* key_to_upgrade,
+                                         const keymaster_key_param_set_t* upgrade_params,
+                                         keymaster_key_blob_t* upgraded_key) const = 0;
     virtual keymaster_error_t begin(keymaster_purpose_t purpose, const keymaster_key_blob_t* key,
                                     const keymaster_key_param_set_t* in_params,
                                     keymaster_key_param_set_t* out_params,
@@ -53,7 +57,7 @@
 
 template <typename T> class KeymasterDevice : public IKeymasterDevice {
   public:
-    KeymasterDevice(T* d) : mDevice{d} {}
+    explicit KeymasterDevice(T* d) : mDevice{d} {}
     keymaster_error_t generate_key(const keymaster_key_param_set_t* params,
                                    keymaster_key_blob_t* key_blob) const override final {
         return mDevice->generate_key(mDevice, params, key_blob, nullptr);
@@ -86,8 +90,13 @@
 
 class Keymaster1Device : public KeymasterDevice<keymaster1_device_t> {
   public:
-    Keymaster1Device(keymaster1_device_t* d) : KeymasterDevice<keymaster1_device_t>{d} {}
+    explicit Keymaster1Device(keymaster1_device_t* d) : KeymasterDevice<keymaster1_device_t>{d} {}
     ~Keymaster1Device() override final { keymaster1_close(mDevice); }
+    keymaster_error_t upgrade_key(const keymaster_key_blob_t* key_to_upgrade,
+                                  const keymaster_key_param_set_t* upgrade_params,
+                                  keymaster_key_blob_t* upgraded_key) const override final {
+        return KM_ERROR_UNIMPLEMENTED;
+    }
     keymaster_error_t finish(keymaster_operation_handle_t operation_handle,
                              const keymaster_key_param_set_t* in_params,
                              const keymaster_blob_t* signature,
@@ -99,8 +108,13 @@
 
 class Keymaster2Device : public KeymasterDevice<keymaster2_device_t> {
   public:
-    Keymaster2Device(keymaster2_device_t* d) : KeymasterDevice<keymaster2_device_t>{d} {}
+    explicit Keymaster2Device(keymaster2_device_t* d) : KeymasterDevice<keymaster2_device_t>{d} {}
     ~Keymaster2Device() override final { keymaster2_close(mDevice); }
+    keymaster_error_t upgrade_key(const keymaster_key_blob_t* key_to_upgrade,
+                                  const keymaster_key_param_set_t* upgrade_params,
+                                  keymaster_key_blob_t* upgraded_key) const override final {
+        return mDevice->upgrade_key(mDevice, key_to_upgrade, upgrade_params, upgraded_key);
+    }
     keymaster_error_t finish(keymaster_operation_handle_t operation_handle,
                              const keymaster_key_param_set_t* in_params,
                              const keymaster_blob_t* signature,
@@ -142,26 +156,19 @@
     return true;
 }
 
-bool KeymasterOperation::finish() {
-    auto error = mDevice->finish(mOpHandle, nullptr, nullptr, nullptr, nullptr);
-    mDevice = nullptr;
-    if (error != KM_ERROR_OK) {
-        LOG(ERROR) << "finish failed, code " << error;
-        return false;
-    }
-    return true;
-}
-
-bool KeymasterOperation::finishWithOutput(std::string* output) {
+bool KeymasterOperation::finish(std::string* output) {
     keymaster_blob_t outputBlob;
-    auto error = mDevice->finish(mOpHandle, nullptr, nullptr, nullptr, &outputBlob);
+    auto error = mDevice->finish(mOpHandle, nullptr, nullptr, nullptr,
+        output ? &outputBlob : nullptr);
     mDevice = nullptr;
     if (error != KM_ERROR_OK) {
         LOG(ERROR) << "finish failed, code " << error;
         return false;
     }
-    output->assign(reinterpret_cast<const char*>(outputBlob.data), outputBlob.data_length);
-    free(const_cast<uint8_t*>(outputBlob.data));
+    if (output) {
+        output->assign(reinterpret_cast<const char*>(outputBlob.data), outputBlob.data_length);
+        free(const_cast<uint8_t*>(outputBlob.data));
+    }
     return true;
 }
 
@@ -173,6 +180,7 @@
         LOG(ERROR) << "hw_get_module_by_class returned " << ret;
         return;
     }
+    LOG(DEBUG) << "module_api_version is " << module->module_api_version;
     if (module->module_api_version == KEYMASTER_MODULE_API_VERSION_1_0) {
         keymaster1_device_t* device;
         ret = keymaster1_open(module, &device);
@@ -188,6 +196,11 @@
             LOG(ERROR) << "keymaster2_open returned " << ret;
             return;
         }
+        auto error = ConfigureDevice(device);
+        if (error != KM_ERROR_OK) {
+            LOG(ERROR) << "ConfigureDevice returned " << error;
+            return;
+        }
         mDevice = std::make_shared<Keymaster2Device>(device);
     } else {
         LOG(ERROR) << "module_api_version is " << module->module_api_version;
@@ -217,31 +230,37 @@
     return true;
 }
 
+bool Keymaster::upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams,
+                           std::string* newKey) {
+    keymaster_key_blob_t oldKeyBlob{reinterpret_cast<const uint8_t*>(oldKey.data()), oldKey.size()};
+    keymaster_key_blob_t newKeyBlob;
+    auto error = mDevice->upgrade_key(&oldKeyBlob, &inParams, &newKeyBlob);
+    if (error != KM_ERROR_OK) {
+        LOG(ERROR) << "upgrade_key failed, code " << error;
+        return false;
+    }
+    newKey->assign(reinterpret_cast<const char*>(newKeyBlob.key_material),
+                   newKeyBlob.key_material_size);
+    free(const_cast<uint8_t*>(newKeyBlob.key_material));
+    return true;
+}
+
 KeymasterOperation Keymaster::begin(keymaster_purpose_t purpose, const std::string& key,
                                     const keymaster::AuthorizationSet& inParams,
                                     keymaster::AuthorizationSet* outParams) {
     keymaster_key_blob_t keyBlob{reinterpret_cast<const uint8_t*>(key.data()), key.size()};
     keymaster_operation_handle_t mOpHandle;
     keymaster_key_param_set_t outParams_set;
-    auto error = mDevice->begin(purpose, &keyBlob, &inParams, &outParams_set, &mOpHandle);
+    auto error = mDevice->begin(purpose, &keyBlob, &inParams,
+        outParams ? &outParams_set : nullptr, &mOpHandle);
     if (error != KM_ERROR_OK) {
         LOG(ERROR) << "begin failed, code " << error;
-        return KeymasterOperation(nullptr, mOpHandle);
+        return KeymasterOperation(error);
     }
-    outParams->Clear();
-    outParams->push_back(outParams_set);
-    keymaster_free_param_set(&outParams_set);
-    return KeymasterOperation(mDevice, mOpHandle);
-}
-
-KeymasterOperation Keymaster::begin(keymaster_purpose_t purpose, const std::string& key,
-                                    const keymaster::AuthorizationSet& inParams) {
-    keymaster_key_blob_t keyBlob{reinterpret_cast<const uint8_t*>(key.data()), key.size()};
-    keymaster_operation_handle_t mOpHandle;
-    auto error = mDevice->begin(purpose, &keyBlob, &inParams, nullptr, &mOpHandle);
-    if (error != KM_ERROR_OK) {
-        LOG(ERROR) << "begin failed, code " << error;
-        return KeymasterOperation(nullptr, mOpHandle);
+    if (outParams) {
+        outParams->Clear();
+        outParams->push_back(outParams_set);
+        keymaster_free_param_set(&outParams_set);
     }
     return KeymasterOperation(mDevice, mOpHandle);
 }
diff --git a/Keymaster.h b/Keymaster.h
index 412110c..e314ee1 100644
--- a/Keymaster.h
+++ b/Keymaster.h
@@ -45,25 +45,31 @@
     ~KeymasterOperation();
     // Is this instance valid? This is false if creation fails, and becomes
     // false on finish or if an update fails.
-    explicit operator bool() { return mDevice != nullptr; }
+    explicit operator bool() { return mError == KM_ERROR_OK; }
+    keymaster_error_t error() { return mError; }
     // Call "update" repeatedly until all of the input is consumed, and
     // concatenate the output. Return true on success.
     bool updateCompletely(const std::string& input, std::string* output);
-    // Finish; pass nullptr for the "output" param.
-    bool finish();
-    // Finish and write the output to this string.
-    bool finishWithOutput(std::string* output);
+    // Finish and write the output to this string, unless pointer is null.
+    bool finish(std::string* output);
     // Move constructor
     KeymasterOperation(KeymasterOperation&& rhs) {
-        mOpHandle = std::move(rhs.mOpHandle);
         mDevice = std::move(rhs.mDevice);
+        mOpHandle = std::move(rhs.mOpHandle);
+        mError = std::move(rhs.mError);
     }
+    // Construct an object in an error state for error returns
+    KeymasterOperation() : KeymasterOperation(KM_ERROR_UNKNOWN_ERROR) {}
 
   private:
     KeymasterOperation(std::shared_ptr<IKeymasterDevice> d, keymaster_operation_handle_t h)
-        : mDevice{d}, mOpHandle{h} {}
+        : mDevice{d}, mOpHandle{h}, mError {KM_ERROR_OK} {}
+    KeymasterOperation(keymaster_error_t error)
+        : mDevice{nullptr}, mOpHandle{static_cast<keymaster_operation_handle_t>(0)},
+          mError {error} {}
     std::shared_ptr<IKeymasterDevice> mDevice;
     keymaster_operation_handle_t mOpHandle;
+    keymaster_error_t mError;
     DISALLOW_COPY_AND_ASSIGN(KeymasterOperation);
     friend class Keymaster;
 };
@@ -79,12 +85,12 @@
     bool generateKey(const AuthorizationSet& inParams, std::string* key);
     // If the keymaster supports it, permanently delete a key.
     bool deleteKey(const std::string& key);
-    // Begin a new cryptographic operation, collecting output parameters.
+    // Replace stored key blob in response to KM_ERROR_KEY_REQUIRES_UPGRADE.
+    bool upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams,
+                    std::string* newKey);
+    // Begin a new cryptographic operation, collecting output parameters if pointer is non-null
     KeymasterOperation begin(keymaster_purpose_t purpose, const std::string& key,
                              const AuthorizationSet& inParams, AuthorizationSet* outParams);
-    // Begin a new cryptographic operation; don't collect output parameters.
-    KeymasterOperation begin(keymaster_purpose_t purpose, const std::string& key,
-                             const AuthorizationSet& inParams);
 
   private:
     std::shared_ptr<IKeymasterDevice> mDevice;
diff --git a/Loop.cpp b/Loop.cpp
index a5863b3..1127817 100644
--- a/Loop.cpp
+++ b/Loop.cpp
@@ -47,7 +47,7 @@
         struct loop_info64 li;
         int rc;
 
-        sprintf(filename, "/dev/block/loop%d", i);
+        snprintf(filename, sizeof(filename), "/dev/block/loop%d", i);
 
         if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
             if (errno != ENOENT) {
@@ -91,7 +91,7 @@
         struct loop_info64 li;
         int rc;
 
-        sprintf(filename, "/dev/block/loop%d", i);
+        snprintf(filename, sizeof(filename), "/dev/block/loop%d", i);
 
         if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
             if (errno != ENOENT) {
@@ -137,7 +137,7 @@
         int rc;
         char *secontext = NULL;
 
-        sprintf(filename, "/dev/block/loop%d", i);
+        snprintf(filename, sizeof(filename), "/dev/block/loop%d", i);
 
         /*
          * The kernel starts us off with 8 loop nodes, but more
diff --git a/MoveTask.cpp b/MoveTask.cpp
index 0a60c4e..38cca04 100644
--- a/MoveTask.cpp
+++ b/MoveTask.cpp
@@ -29,6 +29,8 @@
 
 #define CONSTRAIN(amount, low, high) ((amount) < (low) ? (low) : ((amount) > (high) ? (high) : (amount)))
 
+#define EXEC_BLOCKING 0
+
 using android::base::StringPrintf;
 
 namespace android {
@@ -93,6 +95,9 @@
         return OK;
     }
 
+#if EXEC_BLOCKING
+    return ForkExecvp(cmd);
+#else
     pid_t pid = ForkExecvpAsync(cmd);
     if (pid == -1) return -1;
 
@@ -113,6 +118,7 @@
                 ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress));
     }
     return -1;
+#endif
 }
 
 static status_t execCp(const std::string& fromPath, const std::string& toPath,
@@ -134,6 +140,9 @@
     }
     cmd.push_back(toPath.c_str());
 
+#if EXEC_BLOCKING
+    return ForkExecvp(cmd);
+#else
     pid_t pid = ForkExecvpAsync(cmd);
     if (pid == -1) return -1;
 
@@ -154,6 +163,7 @@
                 ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress));
     }
     return -1;
+#endif
 }
 
 static void bringOffline(const std::shared_ptr<VolumeBase>& vol) {
diff --git a/Process.cpp b/Process.cpp
index 962a460..7dc0144 100644
--- a/Process.cpp
+++ b/Process.cpp
@@ -85,7 +85,7 @@
 
     // compute path to process's directory of open files
     char    path[PATH_MAX];
-    sprintf(path, "/proc/%d/fd", pid);
+    snprintf(path, sizeof(path), "/proc/%d/fd", pid);
     DIR *dir = opendir(path);
     if (!dir)
         return 0;
@@ -129,7 +129,7 @@
     FILE *file;
     char buffer[PATH_MAX + 100];
 
-    sprintf(buffer, "/proc/%d/maps", pid);
+    snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid);
     file = fopen(buffer, "r");
     if (!file)
         return 0;
@@ -155,7 +155,7 @@
     char    path[PATH_MAX];
     char    link[PATH_MAX];
 
-    sprintf(path, "/proc/%d/%s", pid, name);
+    snprintf(path, sizeof(path), "/proc/%d/%s", pid, name);
     if (readSymLink(path, link, sizeof(link)) && pathMatchesMountPoint(link, mountPoint)) 
         return 1;
     return 0;
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index 2085ca8..e51d204 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -584,11 +584,17 @@
                 _exit(0);
             }
             if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage",
-                    NULL, MS_BIND | MS_REC | MS_SLAVE, NULL)) == -1) {
+                    NULL, MS_BIND | MS_REC, NULL)) == -1) {
                 PLOG(ERROR) << "Failed to mount " << storageSource << " for "
                         << de->d_name;
                 _exit(1);
             }
+            if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL,
+                    MS_REC | MS_SLAVE, NULL)) == -1) {
+                PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for "
+                        << de->d_name;
+                _exit(1);
+            }
 
             // Mount user-specific symlink helper into place
             userid_t user_id = multiuser_get_user_id(uid);
@@ -1001,24 +1007,6 @@
 
     oldNumSec = info.st_size / 512;
 
-    unsigned long numImgSectors;
-    if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
-        numImgSectors = adjustSectorNumExt4(numSectors);
-    else
-        numImgSectors = adjustSectorNumFAT(numSectors);
-    /*
-     *  add one block for the superblock
-     */
-    SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1);
-    if (oldNumSec == numImgSectors + 1) {
-        SLOGW("Size unchanged; ignoring resize request");
-        return 0;
-    } else if (oldNumSec > numImgSectors + 1) {
-        SLOGE("Only growing is currently supported.");
-        close(fd);
-        return -1;
-    }
-
     /*
      * Try to read superblock.
      */
@@ -1044,10 +1032,26 @@
         return -1;
     }
 
+    unsigned long numImgSectors;
     if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
         SLOGE("Only ext4 partitions are supported for resize");
         errno = EINVAL;
         return -1;
+    } else {
+        numImgSectors = adjustSectorNumExt4(numSectors);
+    }
+
+    /*
+     *  add one block for the superblock
+     */
+    SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1);
+    if (oldNumSec == numImgSectors + 1) {
+        SLOGW("Size unchanged; ignoring resize request");
+        return 0;
+    } else if (oldNumSec > numImgSectors + 1) {
+        SLOGE("Only growing is currently supported.");
+        close(fd);
+        return -1;
     }
 
     if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
diff --git a/bench/benchgen.py b/bench/benchgen.py
index 9a2bcd7..c852169 100644
--- a/bench/benchgen.py
+++ b/bench/benchgen.py
@@ -183,7 +183,12 @@
                 if handle not in defined:
                     print >>bench, "int ",
                     defined.add(handle)
-                print >>bench, '%s = TEMP_FAILURE_RETRY(open("file%s", %s));' % (handle, f.ident, e.args[2])
+                create_mode = ''
+                if 'O_CREAT' in e.args[2]:
+                    assert len(e.args) > 3, 'File creation lacks a mode?'
+                    create_mode = ', ' + e.args[3]
+                print >>bench, '%s = TEMP_FAILURE_RETRY(open("file%s", %s%s));' \
+                        % (handle, f.ident, e.args[2], create_mode)
 
         elif e.call == "close":
             fd, f, handle = extract_file(e, e.args[0])
@@ -273,7 +278,7 @@
         LOG(ERROR) << "Failed to read random data";
         return -EIO;
     }
-    if ((out = TEMP_FAILURE_RETRY(open(name, O_WRONLY|O_CREAT|O_TRUNC))) < 0) {
+    if ((out = TEMP_FAILURE_RETRY(open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644))) < 0) {
         PLOG(ERROR) << "Failed to open " << name;
         return -errno;
     }
diff --git a/cryptfs.c b/cryptfs.c
index f6698f6..e2606ec 100644
--- a/cryptfs.c
+++ b/cryptfs.c
@@ -1068,6 +1068,7 @@
   struct dm_target_spec *tgt;
   char *crypt_params;
   char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
+  size_t buff_offset;
   int i;
 
   io = (struct dm_ioctl *) buffer;
@@ -1093,8 +1094,11 @@
 
   crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
   convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
-  sprintf(crypt_params, "%s %s 0 %s 0 %s", crypt_ftr->crypto_type_name,
-          master_key_ascii, real_blk_name, extra_params);
+
+  buff_offset = crypt_params - buffer;
+  snprintf(crypt_params, sizeof(buffer) - buff_offset, "%s %s 0 %s 0 %s",
+           crypt_ftr->crypto_type_name, master_key_ascii, real_blk_name,
+           extra_params);
   crypt_params += strlen(crypt_params) + 1;
   crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
   tgt->next = crypt_params - buffer;
@@ -1405,6 +1409,8 @@
       SLOGE("encrypt_master_key: crypto_scrypt failed");
     }
 
+    EVP_CIPHER_CTX_cleanup(&e_ctx);
+
     return 0;
 }
 
@@ -1448,12 +1454,14 @@
   /* Copy intermediate key if needed by params */
   if (intermediate_key && intermediate_key_size) {
     *intermediate_key = (unsigned char*) malloc(KEY_LEN_BYTES);
-    if (intermediate_key) {
+    if (*intermediate_key) {
       memcpy(*intermediate_key, ikey, KEY_LEN_BYTES);
       *intermediate_key_size = KEY_LEN_BYTES;
     }
   }
 
+  EVP_CIPHER_CTX_cleanup(&d_ctx);
+
   return 0;
 }
 
@@ -1733,11 +1741,11 @@
             return -1;
         }
 
-        property_set("vold.decrypt", "trigger_load_persist_props");
         /* Create necessary paths on /data */
         if (prep_data_fs()) {
             return -1;
         }
+        property_set("vold.decrypt", "trigger_load_persist_props");
 
         /* startup service classes main and late_start */
         property_set("vold.decrypt", "trigger_restart_framework");
@@ -1889,7 +1897,8 @@
   } else {
     /* Try mounting the file system anyway, just in case the problem's with
      * the footer, not the key. */
-    sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
+    snprintf(tmp_mount_point, sizeof(tmp_mount_point), "%s/tmp_mnt",
+             mount_point);
     mkdir(tmp_mount_point, 0755);
     if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
       SLOGE("Error temp mounting decrypted block device\n");
diff --git a/fs/Vfat.cpp b/fs/Vfat.cpp
index 38681c9..1803c4b 100644
--- a/fs/Vfat.cpp
+++ b/fs/Vfat.cpp
@@ -139,7 +139,7 @@
     flags |= (ro ? MS_RDONLY : 0);
     flags |= (remount ? MS_REMOUNT : 0);
 
-    sprintf(mountData,
+    snprintf(mountData, sizeof(mountData),
             "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed",
             ownerUid, ownerGid, permMask, permMask);
 
diff --git a/tests/Android.mk b/tests/Android.mk
index f974e7f..416e621 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -7,11 +7,7 @@
 LOCAL_C_INCLUDES := \
     system/core/fs_mgr/include
 
-LOCAL_SHARED_LIBRARIES := \
-    liblog \
-    libcrypto \
-
-LOCAL_STATIC_LIBRARIES := libvold
+LOCAL_STATIC_LIBRARIES := libselinux libvold liblog libcrypto
 LOCAL_SRC_FILES := VolumeManager_test.cpp
 LOCAL_MODULE := vold_tests
 LOCAL_MODULE_TAGS := eng tests