Skip to content

Commit 76777b2

Browse files
committed
[DFSan] Add wrapper for getentropy().
Reviewed By: morehouse Differential Revision: https://reviews.llvm.org/D108604
1 parent 8103b07 commit 76777b2

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

compiler-rt/lib/dfsan/dfsan_custom.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,33 @@ char *__dfso_get_current_dir_name(dfsan_label *ret_label,
10261026
return __dfsw_get_current_dir_name(ret_label);
10271027
}
10281028

1029+
// This function is only available for glibc 2.25 or newer. Mark it weak so
1030+
// linking succeeds with older glibcs.
1031+
SANITIZER_WEAK_ATTRIBUTE int getentropy(void *buffer, size_t length);
1032+
1033+
SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_getentropy(void *buffer, size_t length,
1034+
dfsan_label buffer_label,
1035+
dfsan_label length_label,
1036+
dfsan_label *ret_label) {
1037+
int ret = getentropy(buffer, length);
1038+
if (ret == 0) {
1039+
dfsan_set_label(0, buffer, length);
1040+
}
1041+
*ret_label = 0;
1042+
return ret;
1043+
}
1044+
1045+
SANITIZER_INTERFACE_ATTRIBUTE int __dfso_getentropy(void *buffer, size_t length,
1046+
dfsan_label buffer_label,
1047+
dfsan_label length_label,
1048+
dfsan_label *ret_label,
1049+
dfsan_origin buffer_origin,
1050+
dfsan_origin length_origin,
1051+
dfsan_origin *ret_origin) {
1052+
return __dfsw_getentropy(buffer, length, buffer_label, length_label,
1053+
ret_label);
1054+
}
1055+
10291056
SANITIZER_INTERFACE_ATTRIBUTE
10301057
int __dfsw_gethostname(char *name, size_t len, dfsan_label name_label,
10311058
dfsan_label len_label, dfsan_label *ret_label) {

compiler-rt/lib/dfsan/done_abilist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ fun:fgets=custom
218218
fun:fstat=custom
219219
fun:getcwd=custom
220220
fun:get_current_dir_name=custom
221+
fun:getentropy=custom
221222
fun:gethostname=custom
222223
fun:getpeername=custom
223224
fun:getrlimit=custom

compiler-rt/lib/dfsan/libc_ubuntu1404_abilist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,7 @@ fun:getdirentries64=uninstrumented
18521852
fun:getdomainname=uninstrumented
18531853
fun:getdtablesize=uninstrumented
18541854
fun:getegid=uninstrumented
1855+
fun:getentropy=uninstrumented
18551856
fun:getenv=uninstrumented
18561857
fun:geteuid=uninstrumented
18571858
fun:getfsent=uninstrumented

compiler-rt/test/dfsan/custom.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ dfsan_label i_j_label = 0;
161161
#define ASSERT_SAVED_N_ORIGINS(val, n)
162162
#endif
163163

164+
#if !defined(__GLIBC_PREREQ)
165+
# define __GLIBC_PREREQ(a, b) 0
166+
#endif
167+
164168
void test_stat() {
165169
int i = 1;
166170
dfsan_set_label(i_label, &i, sizeof(i));
@@ -944,6 +948,21 @@ void test_get_current_dir_name() {
944948
ASSERT_ZERO_LABEL(ret);
945949
}
946950

951+
void test_getentropy() {
952+
char buf[64];
953+
dfsan_set_label(i_label, buf + 2, 2);
954+
DEFINE_AND_SAVE_ORIGINS(buf)
955+
#if __GLIBC_PREREQ(2, 25)
956+
// glibc >= 2.25 has getentropy()
957+
int ret = getentropy(buf, sizeof(buf));
958+
ASSERT_ZERO_LABEL(ret);
959+
if (ret == 0) {
960+
ASSERT_READ_ZERO_LABEL(buf + 2, 2);
961+
ASSERT_SAVED_ORIGINS(buf)
962+
}
963+
#endif
964+
}
965+
947966
void test_gethostname() {
948967
char buf[1024];
949968
dfsan_set_label(i_label, buf + 2, 2);
@@ -1967,6 +1986,7 @@ int main(void) {
19671986
test_fstat();
19681987
test_get_current_dir_name();
19691988
test_getcwd();
1989+
test_getentropy();
19701990
test_gethostname();
19711991
test_getpeername();
19721992
test_getpwuid_r();

0 commit comments

Comments
 (0)