- {
- long pers = personality(-1);
- /* 0x40000 aka. ADDR_NO_RANDOMIZE */
- if (!(pers & 0x40000)) {
- if (personality(pers | 0x40000) != -1) {
- /* Use /proc/self/exe instead of trying to figure out the
- * executable path from PATH and argv[0], since that's
- * unreliable. We follow the symlink instead of executing
- * the file directly to avoid top from displaying the
- * name of the process as "exe".
- */
- char runtime[PATH_MAX+1];
- int i = readlink("/proc/self/exe", runtime, PATH_MAX);
- if (i != -1) {
- runtime[i] = '\0';
- execve(runtime, argv, envp);
- }
- }
- /* Either changing the personality or execve() failed. Either
- * way we might as well continue, and hope that the random
- * memory maps are ok this time around.
- */
- fprintf(stderr, "WARNING: Couldn't re-execute SBCL with the proper personality flags (maybe /proc isn't mounted?). Trying to continue anyway.\n");
- }
+ {
+ int pers = personality(0xffffffffUL);
+ /* 0x40000 aka. ADDR_NO_RANDOMIZE */
+ if (!(pers & 0x40000)) {
+ int retval = personality(pers | 0x40000);
+ /* Allegedly some Linux kernels (the reported case was
+ * "hardened Linux 2.6.7") won't set the new personality,
+ * but nor will they return -1 for an error. So as a
+ * workaround query the new personality...
+ */
+ int newpers = personality(0xffffffffUL);
+ /* ... and don't re-execute if either the setting resulted
+ * in an error or if the value didn't change. Otherwise
+ * this might result in an infinite loop.
+ */
+ if (retval != -1 && newpers != pers) {
+ /* Use /proc/self/exe instead of trying to figure out
+ * the executable path from PATH and argv[0], since
+ * that's unreliable. We follow the symlink instead of
+ * executing the file directly in order to prevent top
+ * from displaying the name of the process as "exe". */
+ char runtime[PATH_MAX+1];
+ int i = readlink("/proc/self/exe", runtime, PATH_MAX);
+ if (i != -1) {
+ runtime[i] = '\0';
+ execve(runtime, argv, envp);
+ }
+ }
+ /* Either changing the personality or execve() failed. Either
+ * way we might as well continue, and hope that the random
+ * memory maps are ok this time around.
+ */
+ fprintf(stderr, "WARNING: Couldn't re-execute SBCL with the proper personality flags (maybe /proc isn't mounted?). Trying to continue anyway.\n");
+ }