FORTIFY_SOURCE: add umask check

Verify that the call to umask makes sense. While this wouldn't
have detected bug 7094213 (because the low order bits were all zero),
it might detect other similar bugs.

References: https://code.google.com/p/android-source-browsing/source/detail?r=acba45cc4b1f98f67fcdeda2f7c13ed57659b92a&repo=platform--libcore

Change-Id: I966a531d6b3cf8e1c5eacd69bd3cbec475b5fa58
diff --git a/libc/include/sys/stat.h b/libc/include/sys/stat.h
index bee648e..62e0d8b 100644
--- a/libc/include/sys/stat.h
+++ b/libc/include/sys/stat.h
@@ -122,6 +122,27 @@
 extern int    mknod(const char *, mode_t, dev_t);
 extern mode_t umask(mode_t);
 
+#if defined(__BIONIC_FORTIFY_INLINE)
+
+extern mode_t __umask_chk(mode_t);
+extern mode_t __umask_real(mode_t)
+    __asm__(__USER_LABEL_PREFIX__ "umask");
+extern void __umask_error()
+    __attribute__((__error__("umask called with invalid mode")));
+
+__BIONIC_FORTIFY_INLINE
+mode_t umask(mode_t mode) {
+  if (__builtin_constant_p(mode)) {
+    if ((mode & 0777) != mode) {
+      __umask_error();
+    }
+    return __umask_real(mode);
+  }
+  return __umask_chk(mode);
+}
+#endif /* defined(__BIONIC_FORTIFY_INLINE) */
+
+
 #define  stat64    stat
 #define  fstat64   fstat
 #define  lstat64   lstat