]> granicus.if.org Git - sudo/commitdiff
Use madvise(2) with MADV_WIPEONFORK if available.
authorTodd C. Miller <Todd.Miller@courtesan.com>
Tue, 3 Jul 2018 19:58:49 +0000 (13:58 -0600)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Tue, 3 Jul 2018 19:58:49 +0000 (13:58 -0600)
lib/util/arc4random.h

index ec63c635e68f7372fdb4d837c5ef0a5aabbb79cb..d6490177ebcdb30bec8dd693c7dcfb5874ce7f02 100644 (file)
@@ -63,17 +63,21 @@ _rs_forkhandler(void)
 }
 #endif /* HAVE_PTHREAD_ATFORK */
 
+static int wipeonfork;
+
 static inline void
 _rs_forkdetect(void)
 {
-       static pid_t _rs_pid = 0;
-       pid_t pid = getpid();
-
-       if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) {
-               _rs_pid = pid;
-               _rs_forked = 0;
-               if (rs)
-                       memset(rs, 0, sizeof(*rs));
+       if (!wipeonfork) {
+               static pid_t _rs_pid = 0;
+               pid_t pid = getpid();
+
+               if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) {
+                       _rs_pid = pid;
+                       _rs_forked = 0;
+                       if (rs)
+                               memset(rs, 0, sizeof(*rs));
+               }
        }
 }
 
@@ -91,6 +95,13 @@ _rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
                return (-1);
        }
 
+#ifdef MADV_WIPEONFORK
+       if (madvise (*rsp, sizeof(**rsp), MADV_WIPEONFORK) == 0 &&
+           madvise (*rsxp, sizeof(**rsxp), MADV_WIPEONFORK) == 0) {
+               wipeonfork = 1;
+       }
+#endif
+
        _ARC4_ATFORK(_rs_forkhandler);
        return (0);
 }