On Thu, Oct 8, 2020 at 6:54 PM Topi Miettinen <toiwoton@xxxxxxxxx> wrote: > Writing a new value of 3 to /proc/sys/kernel/randomize_va_space > enables full randomization of memory mappings created with mmap(NULL, > ...). With 2, the base of the VMA used for such mappings is random, > but the mappings are created in predictable places within the VMA and > in sequential order. With 3, new VMAs are created to fully randomize > the mappings. Also mremap(..., MREMAP_MAYMOVE) will move the mappings > even if not necessary. [...] > + if ((flags & MREMAP_MAYMOVE) && randomize_va_space >= 3) { > + /* > + * Caller is happy with a different address, so let's > + * move even if not necessary! > + */ > + new_addr = arch_mmap_rnd(); > + > + ret = mremap_to(addr, old_len, new_addr, new_len, > + &locked, flags, &uf, &uf_unmap_early, > + &uf_unmap); > + goto out; > + } You just pick a random number as the address, and try to place the mapping there? Won't this fail if e.g. the old address range overlaps with the new one, causing mremap_to() to bail out at "if (addr + old_len > new_addr && new_addr + new_len > addr)"? Also, on Linux, the main program stack is (currently) an expanding memory mapping that starts out being something like a couple hundred kilobytes in size. If you allocate memory too close to the main program stack, and someone then recurses deep enough to need more memory, the program will crash. It sounds like your patch will randomly make such programs crash. Also, what's your strategy in general with regards to collisions with existing mappings? Is your intention to just fall back to the classic algorithm in that case? You may want to consider whether it would be better to store information about free memory per subtree in the VMA tree, together with the maximum gap size that is already stored in each node, and then walk down the tree randomly, with the randomness weighted by free memory in the subtrees, but ignoring subtrees whose gaps are too small. And for expanding stacks, it might be a good idea for other reasons as well (locking consistency) to refactor them such that the size in the VMA tree corresponds to the maximum expansion of the stack (and if an allocation is about to fail, shrink such stack mappings).