HomeFreeBSD

pam_zfs_key: malloc and mlock/munlock won't match

Description

pam_zfs_key: malloc and mlock/munlock won't match

mlock(2) and munlock(2) operate on memory pages whereas malloc(3)
does not. So if you munlock(2) a malloced memory region, the whole
page containing it is freed. Since this page may contain another
malloced and mlocked memory region, used as a password buffer by a
concurrent running instance of pam_zfs_key, there is a slight chance
of leaking passwords. By using mmap(2) we avoid such problems since
it will return whole pages on page aligned addresses.

Although the above concern may be mostly academical, it is still
better to use mmap(2) for allocating memory since the FreeBSD
documentation suggests to call mlock(2) and munlock(2) on page
aligned addresses, and other implementations even require it.

While here, remove duplicate code in alloc_pw_string() by calling
alloc_pw_size().

Reviewed-by: Felix Dörre <felix@dogcraft.de>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Attila Fülöp <attila@fueloep.org>
Closes #12665

Details

Provenance
Attila Fülöp <attila@fueloep.org>Authored on Oct 21 2021, 10:17 AM
Brian Behlendorf <behlendorf1@llnl.gov>Committed on Oct 22 2021, 6:42 PM
Parents
rG50292e2545bd: pam_zfs_key: mlock(2) and munlock(2) can fail
Branches
Unknown
Tags
Unknown

Event Timeline

Brian Behlendorf <behlendorf1@llnl.gov> committed rG7cc5cb8083f4: pam_zfs_key: malloc and mlock/munlock won't match (authored by Attila Fülöp <attila@fueloep.org>).Oct 22 2021, 6:42 PM