Skip to content

Commit

Permalink
Auto merge of #2156 - niluxv:add_explicit_memset_functions, r=JohnTitor
Browse files Browse the repository at this point in the history
Add explicit/volatile memset/memzero functions

Adds the following functions to platforms that support them:
* `explicit_bzero` (de facto standard): Glibc, MUSL, FreeBSD, DragonFly BSD, OpenBSD
* `explicit_memset`: NetBSD
* `memset_s` (C11 standard): FreeBSD, DragonFly BSD, Apple OSX

These functions are useful for zeroing secret memory.

Closes #2009
  • Loading branch information
bors committed May 11, 2021
2 parents a0dcb2d + ea18049 commit d3468b2
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions libc-test/semver/apple.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,7 @@ mach_timebase_info_data_t
madvise
max_align_t
mcontext_t
memset_s
mincore
mkdirat
mkstemps
Expand Down
2 changes: 2 additions & 0 deletions libc-test/semver/dragonfly.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,7 @@ endpwent
endservent
endutxent
exit_status
explicit_bzero
faccessat
fchdir
fchflags
Expand Down Expand Up @@ -1243,6 +1244,7 @@ lwpid_t
madvise
memmem
memrchr
memset_s
mincore
mkdirat
mkfifoat
Expand Down
2 changes: 2 additions & 0 deletions libc-test/semver/freebsd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,7 @@ endgrent
endpwent
endservent
endutxent
explicit_bzero
extattr_delete_fd
extattr_delete_file
extattr_delete_link
Expand Down Expand Up @@ -1442,6 +1443,7 @@ lwpid_t
madvise
memmem
memrchr
memset_s
mincore
mkdirat
mkfifoat
Expand Down
1 change: 1 addition & 0 deletions libc-test/semver/linux-gnu.txt
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ copy_file_range
dlinfo
dlmopen
endutxent
explicit_bzero
fgetspent_r
getgrent_r
getpt
Expand Down
1 change: 1 addition & 0 deletions libc-test/semver/linux-musl.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# TODO: musl.
explicit_bzero
1 change: 1 addition & 0 deletions libc-test/semver/netbsd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,7 @@ endpwent
endservent
endutent
endutxent
explicit_memset
extattr_delete_fd
extattr_delete_file
extattr_delete_link
Expand Down
1 change: 1 addition & 0 deletions libc-test/semver/openbsd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,7 @@ endgrent
endpwent
endservent
execvpe
explicit_bzero
export_args
faccessat
fchdir
Expand Down
4 changes: 4 additions & 0 deletions src/unix/bsd/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3877,6 +3877,10 @@ extern "C" {
dst: *const ::c_char,
flags: u32,
) -> ::c_int;

// Added in macOS 10.13
// ISO/IEC 9899:2011 ("ISO C11") K.3.7.4.1
pub fn memset_s(s: *mut ::c_void, smax: ::size_t, c: ::c_int, n: ::size_t) -> ::c_int;
}

#[link(name = "iconv")]
Expand Down
6 changes: 6 additions & 0 deletions src/unix/bsd/freebsdlike/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,12 @@ extern "C" {
outbytesleft: *mut ::size_t,
) -> ::size_t;
pub fn iconv_close(cd: iconv_t) -> ::c_int;

// Added in `FreeBSD` 11.0
// Added in `DragonFly BSD` 5.4
pub fn explicit_bzero(s: *mut ::c_void, len: ::size_t);
// ISO/IEC 9899:2011 ("ISO C11") K.3.7.4.1
pub fn memset_s(s: *mut ::c_void, smax: ::size_t, c: ::c_int, n: ::size_t) -> ::c_int;
}

#[link(name = "rt")]
Expand Down
3 changes: 3 additions & 0 deletions src/unix/bsd/netbsdlike/netbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2083,6 +2083,9 @@ extern "C" {
outbytesleft: *mut ::size_t,
) -> ::size_t;
pub fn iconv_close(cd: iconv_t) -> ::c_int;

// Added in `NetBSD` 7.0
pub fn explicit_memset(b: *mut ::c_void, c: ::c_int, len: ::size_t);
}

#[link(name = "util")]
Expand Down
3 changes: 3 additions & 0 deletions src/unix/bsd/netbsdlike/openbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,9 @@ extern "C" {
pub fn freelocale(loc: ::locale_t);
pub fn newlocale(mask: ::c_int, locale: *const ::c_char, base: ::locale_t) -> ::locale_t;
pub fn duplocale(base: ::locale_t) -> ::locale_t;

// Added in `OpenBSD` 5.5
pub fn explicit_bzero(s: *mut ::c_void, len: ::size_t);
}

cfg_if! {
Expand Down
3 changes: 3 additions & 0 deletions src/unix/linux_like/linux/gnu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,9 @@ extern "C" {
newpath: *const ::c_char,
flags: ::c_uint,
) -> ::c_int;

// Added in `glibc` 2.25
pub fn explicit_bzero(s: *mut ::c_void, len: ::size_t);
}

extern "C" {
Expand Down
3 changes: 3 additions & 0 deletions src/unix/linux_like/linux/musl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,9 @@ extern "C" {
path: *const ::c_char,
) -> ::c_int;
pub fn getauxval(type_: ::c_ulong) -> ::c_ulong;

// Added in `musl` 1.1.20
pub fn explicit_bzero(s: *mut ::c_void, len: ::size_t);
}

cfg_if! {
Expand Down

0 comments on commit d3468b2

Please sign in to comment.