dfsan_custom.cpp 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529
  1. //===-- dfsan_custom.cpp --------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of DataFlowSanitizer.
  10. //
  11. // This file defines the custom functions listed in done_abilist.txt.
  12. //===----------------------------------------------------------------------===//
  13. #include <arpa/inet.h>
  14. #include <assert.h>
  15. #include <ctype.h>
  16. #include <dlfcn.h>
  17. #include <link.h>
  18. #include <poll.h>
  19. #include <pthread.h>
  20. #include <pwd.h>
  21. #include <sched.h>
  22. #include <signal.h>
  23. #include <stdarg.h>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/epoll.h>
  29. #include <sys/resource.h>
  30. #include <sys/select.h>
  31. #include <sys/socket.h>
  32. #include <sys/stat.h>
  33. #include <sys/time.h>
  34. #include <sys/types.h>
  35. #include <time.h>
  36. #include <unistd.h>
  37. #include "dfsan/dfsan.h"
  38. #include "dfsan/dfsan_chained_origin_depot.h"
  39. #include "dfsan/dfsan_flags.h"
  40. #include "dfsan/dfsan_thread.h"
  41. #include "sanitizer_common/sanitizer_common.h"
  42. #include "sanitizer_common/sanitizer_internal_defs.h"
  43. #include "sanitizer_common/sanitizer_linux.h"
  44. #include "sanitizer_common/sanitizer_stackdepot.h"
  45. using namespace __dfsan;
  46. #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) \
  47. do { \
  48. if (f) \
  49. f(__VA_ARGS__); \
  50. } while (false)
  51. #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
  52. SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void f(__VA_ARGS__);
  53. // Async-safe, non-reentrant spin lock.
  54. class SignalSpinLocker {
  55. public:
  56. SignalSpinLocker() {
  57. sigset_t all_set;
  58. sigfillset(&all_set);
  59. pthread_sigmask(SIG_SETMASK, &all_set, &saved_thread_mask_);
  60. sigactions_mu.Lock();
  61. }
  62. ~SignalSpinLocker() {
  63. sigactions_mu.Unlock();
  64. pthread_sigmask(SIG_SETMASK, &saved_thread_mask_, nullptr);
  65. }
  66. private:
  67. static StaticSpinMutex sigactions_mu;
  68. sigset_t saved_thread_mask_;
  69. SignalSpinLocker(const SignalSpinLocker &) = delete;
  70. SignalSpinLocker &operator=(const SignalSpinLocker &) = delete;
  71. };
  72. StaticSpinMutex SignalSpinLocker::sigactions_mu;
  73. extern "C" {
  74. SANITIZER_INTERFACE_ATTRIBUTE int
  75. __dfsw_stat(const char *path, struct stat *buf, dfsan_label path_label,
  76. dfsan_label buf_label, dfsan_label *ret_label) {
  77. int ret = stat(path, buf);
  78. if (ret == 0)
  79. dfsan_set_label(0, buf, sizeof(struct stat));
  80. *ret_label = 0;
  81. return ret;
  82. }
  83. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_stat(
  84. const char *path, struct stat *buf, dfsan_label path_label,
  85. dfsan_label buf_label, dfsan_label *ret_label, dfsan_origin path_origin,
  86. dfsan_origin buf_origin, dfsan_origin *ret_origin) {
  87. int ret = __dfsw_stat(path, buf, path_label, buf_label, ret_label);
  88. return ret;
  89. }
  90. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_fstat(int fd, struct stat *buf,
  91. dfsan_label fd_label,
  92. dfsan_label buf_label,
  93. dfsan_label *ret_label) {
  94. int ret = fstat(fd, buf);
  95. if (ret == 0)
  96. dfsan_set_label(0, buf, sizeof(struct stat));
  97. *ret_label = 0;
  98. return ret;
  99. }
  100. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_fstat(
  101. int fd, struct stat *buf, dfsan_label fd_label, dfsan_label buf_label,
  102. dfsan_label *ret_label, dfsan_origin fd_origin, dfsan_origin buf_origin,
  103. dfsan_origin *ret_origin) {
  104. int ret = __dfsw_fstat(fd, buf, fd_label, buf_label, ret_label);
  105. return ret;
  106. }
  107. static char *dfsan_strchr_with_label(const char *s, int c, size_t *bytes_read,
  108. dfsan_label s_label, dfsan_label c_label,
  109. dfsan_label *ret_label) {
  110. char *match_pos = nullptr;
  111. for (size_t i = 0;; ++i) {
  112. if (s[i] == c || s[i] == 0) {
  113. // If s[i] is the \0 at the end of the string, and \0 is not the
  114. // character we are searching for, then return null.
  115. *bytes_read = i + 1;
  116. match_pos = s[i] == 0 && c != 0 ? nullptr : const_cast<char *>(s + i);
  117. break;
  118. }
  119. }
  120. if (flags().strict_data_dependencies)
  121. *ret_label = s_label;
  122. else
  123. *ret_label = dfsan_union(dfsan_read_label(s, *bytes_read),
  124. dfsan_union(s_label, c_label));
  125. return match_pos;
  126. }
  127. SANITIZER_INTERFACE_ATTRIBUTE char *__dfsw_strchr(const char *s, int c,
  128. dfsan_label s_label,
  129. dfsan_label c_label,
  130. dfsan_label *ret_label) {
  131. size_t bytes_read;
  132. return dfsan_strchr_with_label(s, c, &bytes_read, s_label, c_label,
  133. ret_label);
  134. }
  135. SANITIZER_INTERFACE_ATTRIBUTE char *__dfso_strchr(
  136. const char *s, int c, dfsan_label s_label, dfsan_label c_label,
  137. dfsan_label *ret_label, dfsan_origin s_origin, dfsan_origin c_origin,
  138. dfsan_origin *ret_origin) {
  139. size_t bytes_read;
  140. char *r =
  141. dfsan_strchr_with_label(s, c, &bytes_read, s_label, c_label, ret_label);
  142. if (flags().strict_data_dependencies) {
  143. *ret_origin = s_origin;
  144. } else if (*ret_label) {
  145. dfsan_origin o = dfsan_read_origin_of_first_taint(s, bytes_read);
  146. *ret_origin = o ? o : (s_label ? s_origin : c_origin);
  147. }
  148. return r;
  149. }
  150. SANITIZER_INTERFACE_ATTRIBUTE char *__dfsw_strpbrk(const char *s,
  151. const char *accept,
  152. dfsan_label s_label,
  153. dfsan_label accept_label,
  154. dfsan_label *ret_label) {
  155. const char *ret = strpbrk(s, accept);
  156. if (flags().strict_data_dependencies) {
  157. *ret_label = ret ? s_label : 0;
  158. } else {
  159. size_t s_bytes_read = (ret ? ret - s : strlen(s)) + 1;
  160. *ret_label =
  161. dfsan_union(dfsan_read_label(s, s_bytes_read),
  162. dfsan_union(dfsan_read_label(accept, strlen(accept) + 1),
  163. dfsan_union(s_label, accept_label)));
  164. }
  165. return const_cast<char *>(ret);
  166. }
  167. SANITIZER_INTERFACE_ATTRIBUTE char *__dfso_strpbrk(
  168. const char *s, const char *accept, dfsan_label s_label,
  169. dfsan_label accept_label, dfsan_label *ret_label, dfsan_origin s_origin,
  170. dfsan_origin accept_origin, dfsan_origin *ret_origin) {
  171. const char *ret = __dfsw_strpbrk(s, accept, s_label, accept_label, ret_label);
  172. if (flags().strict_data_dependencies) {
  173. if (ret)
  174. *ret_origin = s_origin;
  175. } else {
  176. if (*ret_label) {
  177. size_t s_bytes_read = (ret ? ret - s : strlen(s)) + 1;
  178. dfsan_origin o = dfsan_read_origin_of_first_taint(s, s_bytes_read);
  179. if (o) {
  180. *ret_origin = o;
  181. } else {
  182. o = dfsan_read_origin_of_first_taint(accept, strlen(accept) + 1);
  183. *ret_origin = o ? o : (s_label ? s_origin : accept_origin);
  184. }
  185. }
  186. }
  187. return const_cast<char *>(ret);
  188. }
  189. static int dfsan_memcmp_bcmp(const void *s1, const void *s2, size_t n,
  190. size_t *bytes_read) {
  191. const char *cs1 = (const char *) s1, *cs2 = (const char *) s2;
  192. for (size_t i = 0; i != n; ++i) {
  193. if (cs1[i] != cs2[i]) {
  194. *bytes_read = i + 1;
  195. return cs1[i] - cs2[i];
  196. }
  197. }
  198. *bytes_read = n;
  199. return 0;
  200. }
  201. static dfsan_label dfsan_get_memcmp_label(const void *s1, const void *s2,
  202. size_t pos) {
  203. if (flags().strict_data_dependencies)
  204. return 0;
  205. return dfsan_union(dfsan_read_label(s1, pos), dfsan_read_label(s2, pos));
  206. }
  207. static void dfsan_get_memcmp_origin(const void *s1, const void *s2, size_t pos,
  208. dfsan_label *ret_label,
  209. dfsan_origin *ret_origin) {
  210. *ret_label = dfsan_get_memcmp_label(s1, s2, pos);
  211. if (*ret_label == 0)
  212. return;
  213. dfsan_origin o = dfsan_read_origin_of_first_taint(s1, pos);
  214. *ret_origin = o ? o : dfsan_read_origin_of_first_taint(s2, pos);
  215. }
  216. static int dfsan_memcmp_bcmp_label(const void *s1, const void *s2, size_t n,
  217. dfsan_label *ret_label) {
  218. size_t bytes_read;
  219. int r = dfsan_memcmp_bcmp(s1, s2, n, &bytes_read);
  220. *ret_label = dfsan_get_memcmp_label(s1, s2, bytes_read);
  221. return r;
  222. }
  223. static int dfsan_memcmp_bcmp_origin(const void *s1, const void *s2, size_t n,
  224. dfsan_label *ret_label,
  225. dfsan_origin *ret_origin) {
  226. size_t bytes_read;
  227. int r = dfsan_memcmp_bcmp(s1, s2, n, &bytes_read);
  228. dfsan_get_memcmp_origin(s1, s2, bytes_read, ret_label, ret_origin);
  229. return r;
  230. }
  231. DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_memcmp, uptr caller_pc,
  232. const void *s1, const void *s2, size_t n,
  233. dfsan_label s1_label, dfsan_label s2_label,
  234. dfsan_label n_label)
  235. DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_origin_memcmp, uptr caller_pc,
  236. const void *s1, const void *s2, size_t n,
  237. dfsan_label s1_label, dfsan_label s2_label,
  238. dfsan_label n_label, dfsan_origin s1_origin,
  239. dfsan_origin s2_origin, dfsan_origin n_origin)
  240. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_memcmp(const void *s1, const void *s2,
  241. size_t n, dfsan_label s1_label,
  242. dfsan_label s2_label,
  243. dfsan_label n_label,
  244. dfsan_label *ret_label) {
  245. CALL_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_memcmp, GET_CALLER_PC(), s1, s2, n,
  246. s1_label, s2_label, n_label);
  247. return dfsan_memcmp_bcmp_label(s1, s2, n, ret_label);
  248. }
  249. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_memcmp(
  250. const void *s1, const void *s2, size_t n, dfsan_label s1_label,
  251. dfsan_label s2_label, dfsan_label n_label, dfsan_label *ret_label,
  252. dfsan_origin s1_origin, dfsan_origin s2_origin, dfsan_origin n_origin,
  253. dfsan_origin *ret_origin) {
  254. CALL_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_origin_memcmp, GET_CALLER_PC(), s1,
  255. s2, n, s1_label, s2_label, n_label, s1_origin,
  256. s2_origin, n_origin);
  257. return dfsan_memcmp_bcmp_origin(s1, s2, n, ret_label, ret_origin);
  258. }
  259. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_bcmp(const void *s1, const void *s2,
  260. size_t n, dfsan_label s1_label,
  261. dfsan_label s2_label,
  262. dfsan_label n_label,
  263. dfsan_label *ret_label) {
  264. return dfsan_memcmp_bcmp_label(s1, s2, n, ret_label);
  265. }
  266. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_bcmp(
  267. const void *s1, const void *s2, size_t n, dfsan_label s1_label,
  268. dfsan_label s2_label, dfsan_label n_label, dfsan_label *ret_label,
  269. dfsan_origin s1_origin, dfsan_origin s2_origin, dfsan_origin n_origin,
  270. dfsan_origin *ret_origin) {
  271. return dfsan_memcmp_bcmp_origin(s1, s2, n, ret_label, ret_origin);
  272. }
  273. // When n == 0, compare strings without byte limit.
  274. // When n > 0, compare the first (at most) n bytes of s1 and s2.
  275. static int dfsan_strncmp(const char *s1, const char *s2, size_t n,
  276. size_t *bytes_read) {
  277. for (size_t i = 0;; ++i) {
  278. if (s1[i] != s2[i] || s1[i] == 0 || s2[i] == 0 || (n > 0 && i == n - 1)) {
  279. *bytes_read = i + 1;
  280. return s1[i] - s2[i];
  281. }
  282. }
  283. }
  284. DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strcmp, uptr caller_pc,
  285. const char *s1, const char *s2,
  286. dfsan_label s1_label, dfsan_label s2_label)
  287. DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_origin_strcmp, uptr caller_pc,
  288. const char *s1, const char *s2,
  289. dfsan_label s1_label, dfsan_label s2_label,
  290. dfsan_origin s1_origin, dfsan_origin s2_origin)
  291. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_strcmp(const char *s1, const char *s2,
  292. dfsan_label s1_label,
  293. dfsan_label s2_label,
  294. dfsan_label *ret_label) {
  295. CALL_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strcmp, GET_CALLER_PC(), s1, s2,
  296. s1_label, s2_label);
  297. size_t bytes_read;
  298. int r = dfsan_strncmp(s1, s2, 0, &bytes_read);
  299. *ret_label = dfsan_get_memcmp_label(s1, s2, bytes_read);
  300. return r;
  301. }
  302. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_strcmp(
  303. const char *s1, const char *s2, dfsan_label s1_label, dfsan_label s2_label,
  304. dfsan_label *ret_label, dfsan_origin s1_origin, dfsan_origin s2_origin,
  305. dfsan_origin *ret_origin) {
  306. CALL_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_origin_strcmp, GET_CALLER_PC(), s1,
  307. s2, s1_label, s2_label, s1_origin, s2_origin);
  308. size_t bytes_read;
  309. int r = dfsan_strncmp(s1, s2, 0, &bytes_read);
  310. dfsan_get_memcmp_origin(s1, s2, bytes_read, ret_label, ret_origin);
  311. return r;
  312. }
  313. // When n == 0, compare strings without byte limit.
  314. // When n > 0, compare the first (at most) n bytes of s1 and s2.
  315. static int dfsan_strncasecmp(const char *s1, const char *s2, size_t n,
  316. size_t *bytes_read) {
  317. for (size_t i = 0;; ++i) {
  318. char s1_lower = tolower(s1[i]);
  319. char s2_lower = tolower(s2[i]);
  320. if (s1_lower != s2_lower || s1[i] == 0 || s2[i] == 0 ||
  321. (n > 0 && i == n - 1)) {
  322. *bytes_read = i + 1;
  323. return s1_lower - s2_lower;
  324. }
  325. }
  326. }
  327. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_strcasecmp(const char *s1,
  328. const char *s2,
  329. dfsan_label s1_label,
  330. dfsan_label s2_label,
  331. dfsan_label *ret_label) {
  332. size_t bytes_read;
  333. int r = dfsan_strncasecmp(s1, s2, 0, &bytes_read);
  334. *ret_label = dfsan_get_memcmp_label(s1, s2, bytes_read);
  335. return r;
  336. }
  337. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_strcasecmp(
  338. const char *s1, const char *s2, dfsan_label s1_label, dfsan_label s2_label,
  339. dfsan_label *ret_label, dfsan_origin s1_origin, dfsan_origin s2_origin,
  340. dfsan_origin *ret_origin) {
  341. size_t bytes_read;
  342. int r = dfsan_strncasecmp(s1, s2, 0, &bytes_read);
  343. dfsan_get_memcmp_origin(s1, s2, bytes_read, ret_label, ret_origin);
  344. return r;
  345. }
  346. DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strncmp, uptr caller_pc,
  347. const char *s1, const char *s2, size_t n,
  348. dfsan_label s1_label, dfsan_label s2_label,
  349. dfsan_label n_label)
  350. DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_origin_strncmp, uptr caller_pc,
  351. const char *s1, const char *s2, size_t n,
  352. dfsan_label s1_label, dfsan_label s2_label,
  353. dfsan_label n_label, dfsan_origin s1_origin,
  354. dfsan_origin s2_origin, dfsan_origin n_origin)
  355. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_strncmp(const char *s1, const char *s2,
  356. size_t n, dfsan_label s1_label,
  357. dfsan_label s2_label,
  358. dfsan_label n_label,
  359. dfsan_label *ret_label) {
  360. if (n == 0) {
  361. *ret_label = 0;
  362. return 0;
  363. }
  364. CALL_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strncmp, GET_CALLER_PC(), s1, s2,
  365. n, s1_label, s2_label, n_label);
  366. size_t bytes_read;
  367. int r = dfsan_strncmp(s1, s2, n, &bytes_read);
  368. *ret_label = dfsan_get_memcmp_label(s1, s2, bytes_read);
  369. return r;
  370. }
  371. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_strncmp(
  372. const char *s1, const char *s2, size_t n, dfsan_label s1_label,
  373. dfsan_label s2_label, dfsan_label n_label, dfsan_label *ret_label,
  374. dfsan_origin s1_origin, dfsan_origin s2_origin, dfsan_origin n_origin,
  375. dfsan_origin *ret_origin) {
  376. if (n == 0) {
  377. *ret_label = 0;
  378. return 0;
  379. }
  380. CALL_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_origin_strncmp, GET_CALLER_PC(),
  381. s1, s2, n, s1_label, s2_label, n_label, s1_origin,
  382. s2_origin, n_origin);
  383. size_t bytes_read;
  384. int r = dfsan_strncmp(s1, s2, n, &bytes_read);
  385. dfsan_get_memcmp_origin(s1, s2, bytes_read, ret_label, ret_origin);
  386. return r;
  387. }
  388. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_strncasecmp(
  389. const char *s1, const char *s2, size_t n, dfsan_label s1_label,
  390. dfsan_label s2_label, dfsan_label n_label, dfsan_label *ret_label) {
  391. if (n == 0) {
  392. *ret_label = 0;
  393. return 0;
  394. }
  395. size_t bytes_read;
  396. int r = dfsan_strncasecmp(s1, s2, n, &bytes_read);
  397. *ret_label = dfsan_get_memcmp_label(s1, s2, bytes_read);
  398. return r;
  399. }
  400. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_strncasecmp(
  401. const char *s1, const char *s2, size_t n, dfsan_label s1_label,
  402. dfsan_label s2_label, dfsan_label n_label, dfsan_label *ret_label,
  403. dfsan_origin s1_origin, dfsan_origin s2_origin, dfsan_origin n_origin,
  404. dfsan_origin *ret_origin) {
  405. if (n == 0) {
  406. *ret_label = 0;
  407. return 0;
  408. }
  409. size_t bytes_read;
  410. int r = dfsan_strncasecmp(s1, s2, n, &bytes_read);
  411. dfsan_get_memcmp_origin(s1, s2, bytes_read, ret_label, ret_origin);
  412. return r;
  413. }
  414. SANITIZER_INTERFACE_ATTRIBUTE size_t
  415. __dfsw_strlen(const char *s, dfsan_label s_label, dfsan_label *ret_label) {
  416. size_t ret = strlen(s);
  417. if (flags().strict_data_dependencies) {
  418. *ret_label = 0;
  419. } else {
  420. *ret_label = dfsan_read_label(s, ret + 1);
  421. }
  422. return ret;
  423. }
  424. SANITIZER_INTERFACE_ATTRIBUTE size_t __dfso_strlen(const char *s,
  425. dfsan_label s_label,
  426. dfsan_label *ret_label,
  427. dfsan_origin s_origin,
  428. dfsan_origin *ret_origin) {
  429. size_t ret = __dfsw_strlen(s, s_label, ret_label);
  430. if (!flags().strict_data_dependencies)
  431. *ret_origin = dfsan_read_origin_of_first_taint(s, ret + 1);
  432. return ret;
  433. }
  434. static void *dfsan_memmove(void *dest, const void *src, size_t n) {
  435. dfsan_label *sdest = shadow_for(dest);
  436. const dfsan_label *ssrc = shadow_for(src);
  437. internal_memmove((void *)sdest, (const void *)ssrc, n * sizeof(dfsan_label));
  438. return internal_memmove(dest, src, n);
  439. }
  440. static void *dfsan_memmove_with_origin(void *dest, const void *src, size_t n) {
  441. dfsan_mem_origin_transfer(dest, src, n);
  442. return dfsan_memmove(dest, src, n);
  443. }
  444. static void *dfsan_memcpy(void *dest, const void *src, size_t n) {
  445. dfsan_mem_shadow_transfer(dest, src, n);
  446. return internal_memcpy(dest, src, n);
  447. }
  448. static void *dfsan_memcpy_with_origin(void *dest, const void *src, size_t n) {
  449. dfsan_mem_origin_transfer(dest, src, n);
  450. return dfsan_memcpy(dest, src, n);
  451. }
  452. static void dfsan_memset(void *s, int c, dfsan_label c_label, size_t n) {
  453. internal_memset(s, c, n);
  454. dfsan_set_label(c_label, s, n);
  455. }
  456. static void dfsan_memset_with_origin(void *s, int c, dfsan_label c_label,
  457. dfsan_origin c_origin, size_t n) {
  458. internal_memset(s, c, n);
  459. dfsan_set_label_origin(c_label, c_origin, s, n);
  460. }
  461. SANITIZER_INTERFACE_ATTRIBUTE
  462. void *__dfsw_memcpy(void *dest, const void *src, size_t n,
  463. dfsan_label dest_label, dfsan_label src_label,
  464. dfsan_label n_label, dfsan_label *ret_label) {
  465. *ret_label = dest_label;
  466. return dfsan_memcpy(dest, src, n);
  467. }
  468. SANITIZER_INTERFACE_ATTRIBUTE
  469. void *__dfso_memcpy(void *dest, const void *src, size_t n,
  470. dfsan_label dest_label, dfsan_label src_label,
  471. dfsan_label n_label, dfsan_label *ret_label,
  472. dfsan_origin dest_origin, dfsan_origin src_origin,
  473. dfsan_origin n_origin, dfsan_origin *ret_origin) {
  474. *ret_label = dest_label;
  475. *ret_origin = dest_origin;
  476. return dfsan_memcpy_with_origin(dest, src, n);
  477. }
  478. SANITIZER_INTERFACE_ATTRIBUTE
  479. void *__dfsw_memmove(void *dest, const void *src, size_t n,
  480. dfsan_label dest_label, dfsan_label src_label,
  481. dfsan_label n_label, dfsan_label *ret_label) {
  482. *ret_label = dest_label;
  483. return dfsan_memmove(dest, src, n);
  484. }
  485. SANITIZER_INTERFACE_ATTRIBUTE
  486. void *__dfso_memmove(void *dest, const void *src, size_t n,
  487. dfsan_label dest_label, dfsan_label src_label,
  488. dfsan_label n_label, dfsan_label *ret_label,
  489. dfsan_origin dest_origin, dfsan_origin src_origin,
  490. dfsan_origin n_origin, dfsan_origin *ret_origin) {
  491. *ret_label = dest_label;
  492. *ret_origin = dest_origin;
  493. return dfsan_memmove_with_origin(dest, src, n);
  494. }
  495. SANITIZER_INTERFACE_ATTRIBUTE
  496. void *__dfsw_memset(void *s, int c, size_t n,
  497. dfsan_label s_label, dfsan_label c_label,
  498. dfsan_label n_label, dfsan_label *ret_label) {
  499. dfsan_memset(s, c, c_label, n);
  500. *ret_label = s_label;
  501. return s;
  502. }
  503. SANITIZER_INTERFACE_ATTRIBUTE
  504. void *__dfso_memset(void *s, int c, size_t n, dfsan_label s_label,
  505. dfsan_label c_label, dfsan_label n_label,
  506. dfsan_label *ret_label, dfsan_origin s_origin,
  507. dfsan_origin c_origin, dfsan_origin n_origin,
  508. dfsan_origin *ret_origin) {
  509. dfsan_memset_with_origin(s, c, c_label, c_origin, n);
  510. *ret_label = s_label;
  511. *ret_origin = s_origin;
  512. return s;
  513. }
  514. SANITIZER_INTERFACE_ATTRIBUTE char *__dfsw_strcat(char *dest, const char *src,
  515. dfsan_label dest_label,
  516. dfsan_label src_label,
  517. dfsan_label *ret_label) {
  518. size_t dest_len = strlen(dest);
  519. char *ret = strcat(dest, src);
  520. dfsan_mem_shadow_transfer(dest + dest_len, src, strlen(src));
  521. *ret_label = dest_label;
  522. return ret;
  523. }
  524. SANITIZER_INTERFACE_ATTRIBUTE char *__dfso_strcat(
  525. char *dest, const char *src, dfsan_label dest_label, dfsan_label src_label,
  526. dfsan_label *ret_label, dfsan_origin dest_origin, dfsan_origin src_origin,
  527. dfsan_origin *ret_origin) {
  528. size_t dest_len = strlen(dest);
  529. char *ret = strcat(dest, src);
  530. size_t src_len = strlen(src);
  531. dfsan_mem_origin_transfer(dest + dest_len, src, src_len);
  532. dfsan_mem_shadow_transfer(dest + dest_len, src, src_len);
  533. *ret_label = dest_label;
  534. *ret_origin = dest_origin;
  535. return ret;
  536. }
  537. SANITIZER_INTERFACE_ATTRIBUTE char *
  538. __dfsw_strdup(const char *s, dfsan_label s_label, dfsan_label *ret_label) {
  539. size_t len = strlen(s);
  540. void *p = malloc(len+1);
  541. dfsan_memcpy(p, s, len+1);
  542. *ret_label = 0;
  543. return static_cast<char *>(p);
  544. }
  545. SANITIZER_INTERFACE_ATTRIBUTE char *__dfso_strdup(const char *s,
  546. dfsan_label s_label,
  547. dfsan_label *ret_label,
  548. dfsan_origin s_origin,
  549. dfsan_origin *ret_origin) {
  550. size_t len = strlen(s);
  551. void *p = malloc(len + 1);
  552. dfsan_memcpy_with_origin(p, s, len + 1);
  553. *ret_label = 0;
  554. return static_cast<char *>(p);
  555. }
  556. SANITIZER_INTERFACE_ATTRIBUTE char *
  557. __dfsw_strncpy(char *s1, const char *s2, size_t n, dfsan_label s1_label,
  558. dfsan_label s2_label, dfsan_label n_label,
  559. dfsan_label *ret_label) {
  560. size_t len = strlen(s2);
  561. if (len < n) {
  562. dfsan_memcpy(s1, s2, len+1);
  563. dfsan_memset(s1+len+1, 0, 0, n-len-1);
  564. } else {
  565. dfsan_memcpy(s1, s2, n);
  566. }
  567. *ret_label = s1_label;
  568. return s1;
  569. }
  570. SANITIZER_INTERFACE_ATTRIBUTE char *__dfso_strncpy(
  571. char *s1, const char *s2, size_t n, dfsan_label s1_label,
  572. dfsan_label s2_label, dfsan_label n_label, dfsan_label *ret_label,
  573. dfsan_origin s1_origin, dfsan_origin s2_origin, dfsan_origin n_origin,
  574. dfsan_origin *ret_origin) {
  575. size_t len = strlen(s2);
  576. if (len < n) {
  577. dfsan_memcpy_with_origin(s1, s2, len + 1);
  578. dfsan_memset_with_origin(s1 + len + 1, 0, 0, 0, n - len - 1);
  579. } else {
  580. dfsan_memcpy_with_origin(s1, s2, n);
  581. }
  582. *ret_label = s1_label;
  583. *ret_origin = s1_origin;
  584. return s1;
  585. }
  586. SANITIZER_INTERFACE_ATTRIBUTE ssize_t
  587. __dfsw_pread(int fd, void *buf, size_t count, off_t offset,
  588. dfsan_label fd_label, dfsan_label buf_label,
  589. dfsan_label count_label, dfsan_label offset_label,
  590. dfsan_label *ret_label) {
  591. ssize_t ret = pread(fd, buf, count, offset);
  592. if (ret > 0)
  593. dfsan_set_label(0, buf, ret);
  594. *ret_label = 0;
  595. return ret;
  596. }
  597. SANITIZER_INTERFACE_ATTRIBUTE ssize_t __dfso_pread(
  598. int fd, void *buf, size_t count, off_t offset, dfsan_label fd_label,
  599. dfsan_label buf_label, dfsan_label count_label, dfsan_label offset_label,
  600. dfsan_label *ret_label, dfsan_origin fd_origin, dfsan_origin buf_origin,
  601. dfsan_origin count_origin, dfsan_label offset_origin,
  602. dfsan_origin *ret_origin) {
  603. return __dfsw_pread(fd, buf, count, offset, fd_label, buf_label, count_label,
  604. offset_label, ret_label);
  605. }
  606. SANITIZER_INTERFACE_ATTRIBUTE ssize_t
  607. __dfsw_read(int fd, void *buf, size_t count,
  608. dfsan_label fd_label, dfsan_label buf_label,
  609. dfsan_label count_label,
  610. dfsan_label *ret_label) {
  611. ssize_t ret = read(fd, buf, count);
  612. if (ret > 0)
  613. dfsan_set_label(0, buf, ret);
  614. *ret_label = 0;
  615. return ret;
  616. }
  617. SANITIZER_INTERFACE_ATTRIBUTE ssize_t __dfso_read(
  618. int fd, void *buf, size_t count, dfsan_label fd_label,
  619. dfsan_label buf_label, dfsan_label count_label, dfsan_label *ret_label,
  620. dfsan_origin fd_origin, dfsan_origin buf_origin, dfsan_origin count_origin,
  621. dfsan_origin *ret_origin) {
  622. return __dfsw_read(fd, buf, count, fd_label, buf_label, count_label,
  623. ret_label);
  624. }
  625. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_clock_gettime(clockid_t clk_id,
  626. struct timespec *tp,
  627. dfsan_label clk_id_label,
  628. dfsan_label tp_label,
  629. dfsan_label *ret_label) {
  630. int ret = clock_gettime(clk_id, tp);
  631. if (ret == 0)
  632. dfsan_set_label(0, tp, sizeof(struct timespec));
  633. *ret_label = 0;
  634. return ret;
  635. }
  636. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_clock_gettime(
  637. clockid_t clk_id, struct timespec *tp, dfsan_label clk_id_label,
  638. dfsan_label tp_label, dfsan_label *ret_label, dfsan_origin clk_id_origin,
  639. dfsan_origin tp_origin, dfsan_origin *ret_origin) {
  640. return __dfsw_clock_gettime(clk_id, tp, clk_id_label, tp_label, ret_label);
  641. }
  642. static void dfsan_set_zero_label(const void *ptr, uptr size) {
  643. dfsan_set_label(0, const_cast<void *>(ptr), size);
  644. }
  645. // dlopen() ultimately calls mmap() down inside the loader, which generally
  646. // doesn't participate in dynamic symbol resolution. Therefore we won't
  647. // intercept its calls to mmap, and we have to hook it here.
  648. SANITIZER_INTERFACE_ATTRIBUTE void *
  649. __dfsw_dlopen(const char *filename, int flag, dfsan_label filename_label,
  650. dfsan_label flag_label, dfsan_label *ret_label) {
  651. void *handle = dlopen(filename, flag);
  652. link_map *map = GET_LINK_MAP_BY_DLOPEN_HANDLE(handle);
  653. if (map)
  654. ForEachMappedRegion(map, dfsan_set_zero_label);
  655. *ret_label = 0;
  656. return handle;
  657. }
  658. SANITIZER_INTERFACE_ATTRIBUTE void *__dfso_dlopen(
  659. const char *filename, int flag, dfsan_label filename_label,
  660. dfsan_label flag_label, dfsan_label *ret_label,
  661. dfsan_origin filename_origin, dfsan_origin flag_origin,
  662. dfsan_origin *ret_origin) {
  663. return __dfsw_dlopen(filename, flag, filename_label, flag_label, ret_label);
  664. }
  665. static void *DFsanThreadStartFunc(void *arg) {
  666. DFsanThread *t = (DFsanThread *)arg;
  667. SetCurrentThread(t);
  668. t->Init();
  669. SetSigProcMask(&t->starting_sigset_, nullptr);
  670. return t->ThreadStart();
  671. }
  672. static int dfsan_pthread_create(pthread_t *thread, const pthread_attr_t *attr,
  673. void *start_routine_trampoline,
  674. void *start_routine, void *arg,
  675. dfsan_label *ret_label,
  676. bool track_origins = false) {
  677. pthread_attr_t myattr;
  678. if (!attr) {
  679. pthread_attr_init(&myattr);
  680. attr = &myattr;
  681. }
  682. // Ensure that the thread stack is large enough to hold all TLS data.
  683. AdjustStackSize((void *)(const_cast<pthread_attr_t *>(attr)));
  684. DFsanThread *t =
  685. DFsanThread::Create(start_routine_trampoline,
  686. (thread_callback_t)start_routine, arg, track_origins);
  687. ScopedBlockSignals block(&t->starting_sigset_);
  688. int res = pthread_create(thread, attr, DFsanThreadStartFunc, t);
  689. if (attr == &myattr)
  690. pthread_attr_destroy(&myattr);
  691. *ret_label = 0;
  692. return res;
  693. }
  694. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_pthread_create(
  695. pthread_t *thread, const pthread_attr_t *attr,
  696. void *(*start_routine_trampoline)(void *, void *, dfsan_label,
  697. dfsan_label *),
  698. void *start_routine, void *arg, dfsan_label thread_label,
  699. dfsan_label attr_label, dfsan_label start_routine_label,
  700. dfsan_label arg_label, dfsan_label *ret_label) {
  701. return dfsan_pthread_create(thread, attr, (void *)start_routine_trampoline,
  702. start_routine, arg, ret_label);
  703. }
  704. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_pthread_create(
  705. pthread_t *thread, const pthread_attr_t *attr,
  706. void *(*start_routine_trampoline)(void *, void *, dfsan_label,
  707. dfsan_label *, dfsan_origin,
  708. dfsan_origin *),
  709. void *start_routine, void *arg, dfsan_label thread_label,
  710. dfsan_label attr_label, dfsan_label start_routine_label,
  711. dfsan_label arg_label, dfsan_label *ret_label, dfsan_origin thread_origin,
  712. dfsan_origin attr_origin, dfsan_origin start_routine_origin,
  713. dfsan_origin arg_origin, dfsan_origin *ret_origin) {
  714. return dfsan_pthread_create(thread, attr, (void *)start_routine_trampoline,
  715. start_routine, arg, ret_label, true);
  716. }
  717. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_pthread_join(pthread_t thread,
  718. void **retval,
  719. dfsan_label thread_label,
  720. dfsan_label retval_label,
  721. dfsan_label *ret_label) {
  722. int ret = pthread_join(thread, retval);
  723. if (ret == 0 && retval)
  724. dfsan_set_label(0, retval, sizeof(*retval));
  725. *ret_label = 0;
  726. return ret;
  727. }
  728. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_pthread_join(
  729. pthread_t thread, void **retval, dfsan_label thread_label,
  730. dfsan_label retval_label, dfsan_label *ret_label,
  731. dfsan_origin thread_origin, dfsan_origin retval_origin,
  732. dfsan_origin *ret_origin) {
  733. return __dfsw_pthread_join(thread, retval, thread_label, retval_label,
  734. ret_label);
  735. }
  736. struct dl_iterate_phdr_info {
  737. int (*callback_trampoline)(void *callback, struct dl_phdr_info *info,
  738. size_t size, void *data, dfsan_label info_label,
  739. dfsan_label size_label, dfsan_label data_label,
  740. dfsan_label *ret_label);
  741. void *callback;
  742. void *data;
  743. };
  744. struct dl_iterate_phdr_origin_info {
  745. int (*callback_trampoline)(void *callback, struct dl_phdr_info *info,
  746. size_t size, void *data, dfsan_label info_label,
  747. dfsan_label size_label, dfsan_label data_label,
  748. dfsan_label *ret_label, dfsan_origin info_origin,
  749. dfsan_origin size_origin, dfsan_origin data_origin,
  750. dfsan_origin *ret_origin);
  751. void *callback;
  752. void *data;
  753. };
  754. int dl_iterate_phdr_cb(struct dl_phdr_info *info, size_t size, void *data) {
  755. dl_iterate_phdr_info *dipi = (dl_iterate_phdr_info *)data;
  756. dfsan_set_label(0, *info);
  757. dfsan_set_label(0, const_cast<char *>(info->dlpi_name),
  758. strlen(info->dlpi_name) + 1);
  759. dfsan_set_label(
  760. 0, const_cast<char *>(reinterpret_cast<const char *>(info->dlpi_phdr)),
  761. sizeof(*info->dlpi_phdr) * info->dlpi_phnum);
  762. dfsan_label ret_label;
  763. return dipi->callback_trampoline(dipi->callback, info, size, dipi->data, 0, 0,
  764. 0, &ret_label);
  765. }
  766. int dl_iterate_phdr_origin_cb(struct dl_phdr_info *info, size_t size,
  767. void *data) {
  768. dl_iterate_phdr_origin_info *dipi = (dl_iterate_phdr_origin_info *)data;
  769. dfsan_set_label(0, *info);
  770. dfsan_set_label(0, const_cast<char *>(info->dlpi_name),
  771. strlen(info->dlpi_name) + 1);
  772. dfsan_set_label(
  773. 0, const_cast<char *>(reinterpret_cast<const char *>(info->dlpi_phdr)),
  774. sizeof(*info->dlpi_phdr) * info->dlpi_phnum);
  775. dfsan_label ret_label;
  776. dfsan_origin ret_origin;
  777. return dipi->callback_trampoline(dipi->callback, info, size, dipi->data, 0, 0,
  778. 0, &ret_label, 0, 0, 0, &ret_origin);
  779. }
  780. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_dl_iterate_phdr(
  781. int (*callback_trampoline)(void *callback, struct dl_phdr_info *info,
  782. size_t size, void *data, dfsan_label info_label,
  783. dfsan_label size_label, dfsan_label data_label,
  784. dfsan_label *ret_label),
  785. void *callback, void *data, dfsan_label callback_label,
  786. dfsan_label data_label, dfsan_label *ret_label) {
  787. dl_iterate_phdr_info dipi = { callback_trampoline, callback, data };
  788. *ret_label = 0;
  789. return dl_iterate_phdr(dl_iterate_phdr_cb, &dipi);
  790. }
  791. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_dl_iterate_phdr(
  792. int (*callback_trampoline)(void *callback, struct dl_phdr_info *info,
  793. size_t size, void *data, dfsan_label info_label,
  794. dfsan_label size_label, dfsan_label data_label,
  795. dfsan_label *ret_label, dfsan_origin info_origin,
  796. dfsan_origin size_origin,
  797. dfsan_origin data_origin,
  798. dfsan_origin *ret_origin),
  799. void *callback, void *data, dfsan_label callback_label,
  800. dfsan_label data_label, dfsan_label *ret_label,
  801. dfsan_origin callback_origin, dfsan_origin data_origin,
  802. dfsan_origin *ret_origin) {
  803. dl_iterate_phdr_origin_info dipi = {callback_trampoline, callback, data};
  804. *ret_label = 0;
  805. return dl_iterate_phdr(dl_iterate_phdr_origin_cb, &dipi);
  806. }
  807. // This function is only available for glibc 2.27 or newer. Mark it weak so
  808. // linking succeeds with older glibcs.
  809. SANITIZER_WEAK_ATTRIBUTE void _dl_get_tls_static_info(size_t *sizep,
  810. size_t *alignp);
  811. SANITIZER_INTERFACE_ATTRIBUTE void __dfsw__dl_get_tls_static_info(
  812. size_t *sizep, size_t *alignp, dfsan_label sizep_label,
  813. dfsan_label alignp_label) {
  814. assert(_dl_get_tls_static_info);
  815. _dl_get_tls_static_info(sizep, alignp);
  816. dfsan_set_label(0, sizep, sizeof(*sizep));
  817. dfsan_set_label(0, alignp, sizeof(*alignp));
  818. }
  819. SANITIZER_INTERFACE_ATTRIBUTE void __dfso__dl_get_tls_static_info(
  820. size_t *sizep, size_t *alignp, dfsan_label sizep_label,
  821. dfsan_label alignp_label, dfsan_origin sizep_origin,
  822. dfsan_origin alignp_origin) {
  823. __dfsw__dl_get_tls_static_info(sizep, alignp, sizep_label, alignp_label);
  824. }
  825. SANITIZER_INTERFACE_ATTRIBUTE
  826. char *__dfsw_ctime_r(const time_t *timep, char *buf, dfsan_label timep_label,
  827. dfsan_label buf_label, dfsan_label *ret_label) {
  828. char *ret = ctime_r(timep, buf);
  829. if (ret) {
  830. dfsan_set_label(dfsan_read_label(timep, sizeof(time_t)), buf,
  831. strlen(buf) + 1);
  832. *ret_label = buf_label;
  833. } else {
  834. *ret_label = 0;
  835. }
  836. return ret;
  837. }
  838. SANITIZER_INTERFACE_ATTRIBUTE
  839. char *__dfso_ctime_r(const time_t *timep, char *buf, dfsan_label timep_label,
  840. dfsan_label buf_label, dfsan_label *ret_label,
  841. dfsan_origin timep_origin, dfsan_origin buf_origin,
  842. dfsan_origin *ret_origin) {
  843. char *ret = ctime_r(timep, buf);
  844. if (ret) {
  845. dfsan_set_label_origin(
  846. dfsan_read_label(timep, sizeof(time_t)),
  847. dfsan_read_origin_of_first_taint(timep, sizeof(time_t)), buf,
  848. strlen(buf) + 1);
  849. *ret_label = buf_label;
  850. *ret_origin = buf_origin;
  851. } else {
  852. *ret_label = 0;
  853. }
  854. return ret;
  855. }
  856. SANITIZER_INTERFACE_ATTRIBUTE
  857. char *__dfsw_fgets(char *s, int size, FILE *stream, dfsan_label s_label,
  858. dfsan_label size_label, dfsan_label stream_label,
  859. dfsan_label *ret_label) {
  860. char *ret = fgets(s, size, stream);
  861. if (ret) {
  862. dfsan_set_label(0, ret, strlen(ret) + 1);
  863. *ret_label = s_label;
  864. } else {
  865. *ret_label = 0;
  866. }
  867. return ret;
  868. }
  869. SANITIZER_INTERFACE_ATTRIBUTE
  870. char *__dfso_fgets(char *s, int size, FILE *stream, dfsan_label s_label,
  871. dfsan_label size_label, dfsan_label stream_label,
  872. dfsan_label *ret_label, dfsan_origin s_origin,
  873. dfsan_origin size_origin, dfsan_origin stream_origin,
  874. dfsan_origin *ret_origin) {
  875. char *ret = __dfsw_fgets(s, size, stream, s_label, size_label, stream_label,
  876. ret_label);
  877. if (ret)
  878. *ret_origin = s_origin;
  879. return ret;
  880. }
  881. SANITIZER_INTERFACE_ATTRIBUTE
  882. char *__dfsw_getcwd(char *buf, size_t size, dfsan_label buf_label,
  883. dfsan_label size_label, dfsan_label *ret_label) {
  884. char *ret = getcwd(buf, size);
  885. if (ret) {
  886. dfsan_set_label(0, ret, strlen(ret) + 1);
  887. *ret_label = buf_label;
  888. } else {
  889. *ret_label = 0;
  890. }
  891. return ret;
  892. }
  893. SANITIZER_INTERFACE_ATTRIBUTE
  894. char *__dfso_getcwd(char *buf, size_t size, dfsan_label buf_label,
  895. dfsan_label size_label, dfsan_label *ret_label,
  896. dfsan_origin buf_origin, dfsan_origin size_origin,
  897. dfsan_origin *ret_origin) {
  898. char *ret = __dfsw_getcwd(buf, size, buf_label, size_label, ret_label);
  899. if (ret)
  900. *ret_origin = buf_origin;
  901. return ret;
  902. }
  903. SANITIZER_INTERFACE_ATTRIBUTE
  904. char *__dfsw_get_current_dir_name(dfsan_label *ret_label) {
  905. char *ret = get_current_dir_name();
  906. if (ret)
  907. dfsan_set_label(0, ret, strlen(ret) + 1);
  908. *ret_label = 0;
  909. return ret;
  910. }
  911. SANITIZER_INTERFACE_ATTRIBUTE
  912. char *__dfso_get_current_dir_name(dfsan_label *ret_label,
  913. dfsan_origin *ret_origin) {
  914. return __dfsw_get_current_dir_name(ret_label);
  915. }
  916. // This function is only available for glibc 2.25 or newer. Mark it weak so
  917. // linking succeeds with older glibcs.
  918. SANITIZER_WEAK_ATTRIBUTE int getentropy(void *buffer, size_t length);
  919. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_getentropy(void *buffer, size_t length,
  920. dfsan_label buffer_label,
  921. dfsan_label length_label,
  922. dfsan_label *ret_label) {
  923. int ret = getentropy(buffer, length);
  924. if (ret == 0) {
  925. dfsan_set_label(0, buffer, length);
  926. }
  927. *ret_label = 0;
  928. return ret;
  929. }
  930. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_getentropy(void *buffer, size_t length,
  931. dfsan_label buffer_label,
  932. dfsan_label length_label,
  933. dfsan_label *ret_label,
  934. dfsan_origin buffer_origin,
  935. dfsan_origin length_origin,
  936. dfsan_origin *ret_origin) {
  937. return __dfsw_getentropy(buffer, length, buffer_label, length_label,
  938. ret_label);
  939. }
  940. SANITIZER_INTERFACE_ATTRIBUTE
  941. int __dfsw_gethostname(char *name, size_t len, dfsan_label name_label,
  942. dfsan_label len_label, dfsan_label *ret_label) {
  943. int ret = gethostname(name, len);
  944. if (ret == 0) {
  945. dfsan_set_label(0, name, strlen(name) + 1);
  946. }
  947. *ret_label = 0;
  948. return ret;
  949. }
  950. SANITIZER_INTERFACE_ATTRIBUTE
  951. int __dfso_gethostname(char *name, size_t len, dfsan_label name_label,
  952. dfsan_label len_label, dfsan_label *ret_label,
  953. dfsan_origin name_origin, dfsan_origin len_origin,
  954. dfsan_label *ret_origin) {
  955. return __dfsw_gethostname(name, len, name_label, len_label, ret_label);
  956. }
  957. SANITIZER_INTERFACE_ATTRIBUTE
  958. int __dfsw_getrlimit(int resource, struct rlimit *rlim,
  959. dfsan_label resource_label, dfsan_label rlim_label,
  960. dfsan_label *ret_label) {
  961. int ret = getrlimit(resource, rlim);
  962. if (ret == 0) {
  963. dfsan_set_label(0, rlim, sizeof(struct rlimit));
  964. }
  965. *ret_label = 0;
  966. return ret;
  967. }
  968. SANITIZER_INTERFACE_ATTRIBUTE
  969. int __dfso_getrlimit(int resource, struct rlimit *rlim,
  970. dfsan_label resource_label, dfsan_label rlim_label,
  971. dfsan_label *ret_label, dfsan_origin resource_origin,
  972. dfsan_origin rlim_origin, dfsan_origin *ret_origin) {
  973. return __dfsw_getrlimit(resource, rlim, resource_label, rlim_label,
  974. ret_label);
  975. }
  976. SANITIZER_INTERFACE_ATTRIBUTE
  977. int __dfsw_getrusage(int who, struct rusage *usage, dfsan_label who_label,
  978. dfsan_label usage_label, dfsan_label *ret_label) {
  979. int ret = getrusage(who, usage);
  980. if (ret == 0) {
  981. dfsan_set_label(0, usage, sizeof(struct rusage));
  982. }
  983. *ret_label = 0;
  984. return ret;
  985. }
  986. SANITIZER_INTERFACE_ATTRIBUTE
  987. int __dfso_getrusage(int who, struct rusage *usage, dfsan_label who_label,
  988. dfsan_label usage_label, dfsan_label *ret_label,
  989. dfsan_origin who_origin, dfsan_origin usage_origin,
  990. dfsan_label *ret_origin) {
  991. return __dfsw_getrusage(who, usage, who_label, usage_label, ret_label);
  992. }
  993. SANITIZER_INTERFACE_ATTRIBUTE
  994. char *__dfsw_strcpy(char *dest, const char *src, dfsan_label dst_label,
  995. dfsan_label src_label, dfsan_label *ret_label) {
  996. char *ret = strcpy(dest, src);
  997. if (ret) {
  998. dfsan_mem_shadow_transfer(dest, src, strlen(src) + 1);
  999. }
  1000. *ret_label = dst_label;
  1001. return ret;
  1002. }
  1003. SANITIZER_INTERFACE_ATTRIBUTE
  1004. char *__dfso_strcpy(char *dest, const char *src, dfsan_label dst_label,
  1005. dfsan_label src_label, dfsan_label *ret_label,
  1006. dfsan_origin dst_origin, dfsan_origin src_origin,
  1007. dfsan_origin *ret_origin) {
  1008. char *ret = strcpy(dest, src);
  1009. if (ret) {
  1010. size_t str_len = strlen(src) + 1;
  1011. dfsan_mem_origin_transfer(dest, src, str_len);
  1012. dfsan_mem_shadow_transfer(dest, src, str_len);
  1013. }
  1014. *ret_label = dst_label;
  1015. *ret_origin = dst_origin;
  1016. return ret;
  1017. }
  1018. static long int dfsan_strtol(const char *nptr, char **endptr, int base,
  1019. char **tmp_endptr) {
  1020. assert(tmp_endptr);
  1021. long int ret = strtol(nptr, tmp_endptr, base);
  1022. if (endptr)
  1023. *endptr = *tmp_endptr;
  1024. return ret;
  1025. }
  1026. static void dfsan_strtolong_label(const char *nptr, const char *tmp_endptr,
  1027. dfsan_label base_label,
  1028. dfsan_label *ret_label) {
  1029. if (tmp_endptr > nptr) {
  1030. // If *tmp_endptr is '\0' include its label as well.
  1031. *ret_label = dfsan_union(
  1032. base_label,
  1033. dfsan_read_label(nptr, tmp_endptr - nptr + (*tmp_endptr ? 0 : 1)));
  1034. } else {
  1035. *ret_label = 0;
  1036. }
  1037. }
  1038. static void dfsan_strtolong_origin(const char *nptr, const char *tmp_endptr,
  1039. dfsan_label base_label,
  1040. dfsan_label *ret_label,
  1041. dfsan_origin base_origin,
  1042. dfsan_origin *ret_origin) {
  1043. if (tmp_endptr > nptr) {
  1044. // When multiple inputs are tainted, we propagate one of its origins.
  1045. // Because checking if base_label is tainted does not need additional
  1046. // computation, we prefer to propagating base_origin.
  1047. *ret_origin = base_label
  1048. ? base_origin
  1049. : dfsan_read_origin_of_first_taint(
  1050. nptr, tmp_endptr - nptr + (*tmp_endptr ? 0 : 1));
  1051. }
  1052. }
  1053. SANITIZER_INTERFACE_ATTRIBUTE
  1054. long int __dfsw_strtol(const char *nptr, char **endptr, int base,
  1055. dfsan_label nptr_label, dfsan_label endptr_label,
  1056. dfsan_label base_label, dfsan_label *ret_label) {
  1057. char *tmp_endptr;
  1058. long int ret = dfsan_strtol(nptr, endptr, base, &tmp_endptr);
  1059. dfsan_strtolong_label(nptr, tmp_endptr, base_label, ret_label);
  1060. return ret;
  1061. }
  1062. SANITIZER_INTERFACE_ATTRIBUTE
  1063. long int __dfso_strtol(const char *nptr, char **endptr, int base,
  1064. dfsan_label nptr_label, dfsan_label endptr_label,
  1065. dfsan_label base_label, dfsan_label *ret_label,
  1066. dfsan_origin nptr_origin, dfsan_origin endptr_origin,
  1067. dfsan_origin base_origin, dfsan_origin *ret_origin) {
  1068. char *tmp_endptr;
  1069. long int ret = dfsan_strtol(nptr, endptr, base, &tmp_endptr);
  1070. dfsan_strtolong_label(nptr, tmp_endptr, base_label, ret_label);
  1071. dfsan_strtolong_origin(nptr, tmp_endptr, base_label, ret_label, base_origin,
  1072. ret_origin);
  1073. return ret;
  1074. }
  1075. static double dfsan_strtod(const char *nptr, char **endptr, char **tmp_endptr) {
  1076. assert(tmp_endptr);
  1077. double ret = strtod(nptr, tmp_endptr);
  1078. if (endptr)
  1079. *endptr = *tmp_endptr;
  1080. return ret;
  1081. }
  1082. static void dfsan_strtod_label(const char *nptr, const char *tmp_endptr,
  1083. dfsan_label *ret_label) {
  1084. if (tmp_endptr > nptr) {
  1085. // If *tmp_endptr is '\0' include its label as well.
  1086. *ret_label = dfsan_read_label(
  1087. nptr,
  1088. tmp_endptr - nptr + (*tmp_endptr ? 0 : 1));
  1089. } else {
  1090. *ret_label = 0;
  1091. }
  1092. }
  1093. SANITIZER_INTERFACE_ATTRIBUTE
  1094. double __dfsw_strtod(const char *nptr, char **endptr, dfsan_label nptr_label,
  1095. dfsan_label endptr_label, dfsan_label *ret_label) {
  1096. char *tmp_endptr;
  1097. double ret = dfsan_strtod(nptr, endptr, &tmp_endptr);
  1098. dfsan_strtod_label(nptr, tmp_endptr, ret_label);
  1099. return ret;
  1100. }
  1101. SANITIZER_INTERFACE_ATTRIBUTE
  1102. double __dfso_strtod(const char *nptr, char **endptr, dfsan_label nptr_label,
  1103. dfsan_label endptr_label, dfsan_label *ret_label,
  1104. dfsan_origin nptr_origin, dfsan_origin endptr_origin,
  1105. dfsan_origin *ret_origin) {
  1106. char *tmp_endptr;
  1107. double ret = dfsan_strtod(nptr, endptr, &tmp_endptr);
  1108. dfsan_strtod_label(nptr, tmp_endptr, ret_label);
  1109. if (tmp_endptr > nptr) {
  1110. // If *tmp_endptr is '\0' include its label as well.
  1111. *ret_origin = dfsan_read_origin_of_first_taint(
  1112. nptr, tmp_endptr - nptr + (*tmp_endptr ? 0 : 1));
  1113. } else {
  1114. *ret_origin = 0;
  1115. }
  1116. return ret;
  1117. }
  1118. static long long int dfsan_strtoll(const char *nptr, char **endptr, int base,
  1119. char **tmp_endptr) {
  1120. assert(tmp_endptr);
  1121. long long int ret = strtoll(nptr, tmp_endptr, base);
  1122. if (endptr)
  1123. *endptr = *tmp_endptr;
  1124. return ret;
  1125. }
  1126. SANITIZER_INTERFACE_ATTRIBUTE
  1127. long long int __dfsw_strtoll(const char *nptr, char **endptr, int base,
  1128. dfsan_label nptr_label, dfsan_label endptr_label,
  1129. dfsan_label base_label, dfsan_label *ret_label) {
  1130. char *tmp_endptr;
  1131. long long int ret = dfsan_strtoll(nptr, endptr, base, &tmp_endptr);
  1132. dfsan_strtolong_label(nptr, tmp_endptr, base_label, ret_label);
  1133. return ret;
  1134. }
  1135. SANITIZER_INTERFACE_ATTRIBUTE
  1136. long long int __dfso_strtoll(const char *nptr, char **endptr, int base,
  1137. dfsan_label nptr_label, dfsan_label endptr_label,
  1138. dfsan_label base_label, dfsan_label *ret_label,
  1139. dfsan_origin nptr_origin,
  1140. dfsan_origin endptr_origin,
  1141. dfsan_origin base_origin,
  1142. dfsan_origin *ret_origin) {
  1143. char *tmp_endptr;
  1144. long long int ret = dfsan_strtoll(nptr, endptr, base, &tmp_endptr);
  1145. dfsan_strtolong_label(nptr, tmp_endptr, base_label, ret_label);
  1146. dfsan_strtolong_origin(nptr, tmp_endptr, base_label, ret_label, base_origin,
  1147. ret_origin);
  1148. return ret;
  1149. }
  1150. static unsigned long int dfsan_strtoul(const char *nptr, char **endptr,
  1151. int base, char **tmp_endptr) {
  1152. assert(tmp_endptr);
  1153. unsigned long int ret = strtoul(nptr, tmp_endptr, base);
  1154. if (endptr)
  1155. *endptr = *tmp_endptr;
  1156. return ret;
  1157. }
  1158. SANITIZER_INTERFACE_ATTRIBUTE
  1159. unsigned long int __dfsw_strtoul(const char *nptr, char **endptr, int base,
  1160. dfsan_label nptr_label, dfsan_label endptr_label,
  1161. dfsan_label base_label, dfsan_label *ret_label) {
  1162. char *tmp_endptr;
  1163. unsigned long int ret = dfsan_strtoul(nptr, endptr, base, &tmp_endptr);
  1164. dfsan_strtolong_label(nptr, tmp_endptr, base_label, ret_label);
  1165. return ret;
  1166. }
  1167. SANITIZER_INTERFACE_ATTRIBUTE
  1168. unsigned long int __dfso_strtoul(
  1169. const char *nptr, char **endptr, int base, dfsan_label nptr_label,
  1170. dfsan_label endptr_label, dfsan_label base_label, dfsan_label *ret_label,
  1171. dfsan_origin nptr_origin, dfsan_origin endptr_origin,
  1172. dfsan_origin base_origin, dfsan_origin *ret_origin) {
  1173. char *tmp_endptr;
  1174. unsigned long int ret = dfsan_strtoul(nptr, endptr, base, &tmp_endptr);
  1175. dfsan_strtolong_label(nptr, tmp_endptr, base_label, ret_label);
  1176. dfsan_strtolong_origin(nptr, tmp_endptr, base_label, ret_label, base_origin,
  1177. ret_origin);
  1178. return ret;
  1179. }
  1180. static long long unsigned int dfsan_strtoull(const char *nptr, char **endptr,
  1181. int base, char **tmp_endptr) {
  1182. assert(tmp_endptr);
  1183. long long unsigned int ret = strtoull(nptr, tmp_endptr, base);
  1184. if (endptr)
  1185. *endptr = *tmp_endptr;
  1186. return ret;
  1187. }
  1188. SANITIZER_INTERFACE_ATTRIBUTE
  1189. long long unsigned int __dfsw_strtoull(const char *nptr, char **endptr,
  1190. int base, dfsan_label nptr_label,
  1191. dfsan_label endptr_label,
  1192. dfsan_label base_label,
  1193. dfsan_label *ret_label) {
  1194. char *tmp_endptr;
  1195. long long unsigned int ret = dfsan_strtoull(nptr, endptr, base, &tmp_endptr);
  1196. dfsan_strtolong_label(nptr, tmp_endptr, base_label, ret_label);
  1197. return ret;
  1198. }
  1199. SANITIZER_INTERFACE_ATTRIBUTE
  1200. long long unsigned int __dfso_strtoull(
  1201. const char *nptr, char **endptr, int base, dfsan_label nptr_label,
  1202. dfsan_label endptr_label, dfsan_label base_label, dfsan_label *ret_label,
  1203. dfsan_origin nptr_origin, dfsan_origin endptr_origin,
  1204. dfsan_origin base_origin, dfsan_origin *ret_origin) {
  1205. char *tmp_endptr;
  1206. long long unsigned int ret = dfsan_strtoull(nptr, endptr, base, &tmp_endptr);
  1207. dfsan_strtolong_label(nptr, tmp_endptr, base_label, ret_label);
  1208. dfsan_strtolong_origin(nptr, tmp_endptr, base_label, ret_label, base_origin,
  1209. ret_origin);
  1210. return ret;
  1211. }
  1212. SANITIZER_INTERFACE_ATTRIBUTE
  1213. time_t __dfsw_time(time_t *t, dfsan_label t_label, dfsan_label *ret_label) {
  1214. time_t ret = time(t);
  1215. if (ret != (time_t) -1 && t) {
  1216. dfsan_set_label(0, t, sizeof(time_t));
  1217. }
  1218. *ret_label = 0;
  1219. return ret;
  1220. }
  1221. SANITIZER_INTERFACE_ATTRIBUTE
  1222. time_t __dfso_time(time_t *t, dfsan_label t_label, dfsan_label *ret_label,
  1223. dfsan_origin t_origin, dfsan_origin *ret_origin) {
  1224. return __dfsw_time(t, t_label, ret_label);
  1225. }
  1226. SANITIZER_INTERFACE_ATTRIBUTE
  1227. int __dfsw_inet_pton(int af, const char *src, void *dst, dfsan_label af_label,
  1228. dfsan_label src_label, dfsan_label dst_label,
  1229. dfsan_label *ret_label) {
  1230. int ret = inet_pton(af, src, dst);
  1231. if (ret == 1) {
  1232. dfsan_set_label(dfsan_read_label(src, strlen(src) + 1), dst,
  1233. af == AF_INET ? sizeof(struct in_addr) : sizeof(in6_addr));
  1234. }
  1235. *ret_label = 0;
  1236. return ret;
  1237. }
  1238. SANITIZER_INTERFACE_ATTRIBUTE
  1239. int __dfso_inet_pton(int af, const char *src, void *dst, dfsan_label af_label,
  1240. dfsan_label src_label, dfsan_label dst_label,
  1241. dfsan_label *ret_label, dfsan_origin af_origin,
  1242. dfsan_origin src_origin, dfsan_origin dst_origin,
  1243. dfsan_origin *ret_origin) {
  1244. int ret = inet_pton(af, src, dst);
  1245. if (ret == 1) {
  1246. int src_len = strlen(src) + 1;
  1247. dfsan_set_label_origin(
  1248. dfsan_read_label(src, src_len),
  1249. dfsan_read_origin_of_first_taint(src, src_len), dst,
  1250. af == AF_INET ? sizeof(struct in_addr) : sizeof(in6_addr));
  1251. }
  1252. *ret_label = 0;
  1253. return ret;
  1254. }
  1255. SANITIZER_INTERFACE_ATTRIBUTE
  1256. struct tm *__dfsw_localtime_r(const time_t *timep, struct tm *result,
  1257. dfsan_label timep_label, dfsan_label result_label,
  1258. dfsan_label *ret_label) {
  1259. struct tm *ret = localtime_r(timep, result);
  1260. if (ret) {
  1261. dfsan_set_label(dfsan_read_label(timep, sizeof(time_t)), result,
  1262. sizeof(struct tm));
  1263. *ret_label = result_label;
  1264. } else {
  1265. *ret_label = 0;
  1266. }
  1267. return ret;
  1268. }
  1269. SANITIZER_INTERFACE_ATTRIBUTE
  1270. struct tm *__dfso_localtime_r(const time_t *timep, struct tm *result,
  1271. dfsan_label timep_label, dfsan_label result_label,
  1272. dfsan_label *ret_label, dfsan_origin timep_origin,
  1273. dfsan_origin result_origin,
  1274. dfsan_origin *ret_origin) {
  1275. struct tm *ret = localtime_r(timep, result);
  1276. if (ret) {
  1277. dfsan_set_label_origin(
  1278. dfsan_read_label(timep, sizeof(time_t)),
  1279. dfsan_read_origin_of_first_taint(timep, sizeof(time_t)), result,
  1280. sizeof(struct tm));
  1281. *ret_label = result_label;
  1282. *ret_origin = result_origin;
  1283. } else {
  1284. *ret_label = 0;
  1285. }
  1286. return ret;
  1287. }
  1288. SANITIZER_INTERFACE_ATTRIBUTE
  1289. int __dfsw_getpwuid_r(id_t uid, struct passwd *pwd,
  1290. char *buf, size_t buflen, struct passwd **result,
  1291. dfsan_label uid_label, dfsan_label pwd_label,
  1292. dfsan_label buf_label, dfsan_label buflen_label,
  1293. dfsan_label result_label, dfsan_label *ret_label) {
  1294. // Store the data in pwd, the strings referenced from pwd in buf, and the
  1295. // address of pwd in *result. On failure, NULL is stored in *result.
  1296. int ret = getpwuid_r(uid, pwd, buf, buflen, result);
  1297. if (ret == 0) {
  1298. dfsan_set_label(0, pwd, sizeof(struct passwd));
  1299. dfsan_set_label(0, buf, strlen(buf) + 1);
  1300. }
  1301. *ret_label = 0;
  1302. dfsan_set_label(0, result, sizeof(struct passwd*));
  1303. return ret;
  1304. }
  1305. SANITIZER_INTERFACE_ATTRIBUTE
  1306. int __dfso_getpwuid_r(id_t uid, struct passwd *pwd, char *buf, size_t buflen,
  1307. struct passwd **result, dfsan_label uid_label,
  1308. dfsan_label pwd_label, dfsan_label buf_label,
  1309. dfsan_label buflen_label, dfsan_label result_label,
  1310. dfsan_label *ret_label, dfsan_origin uid_origin,
  1311. dfsan_origin pwd_origin, dfsan_origin buf_origin,
  1312. dfsan_origin buflen_origin, dfsan_origin result_origin,
  1313. dfsan_origin *ret_origin) {
  1314. return __dfsw_getpwuid_r(uid, pwd, buf, buflen, result, uid_label, pwd_label,
  1315. buf_label, buflen_label, result_label, ret_label);
  1316. }
  1317. SANITIZER_INTERFACE_ATTRIBUTE
  1318. int __dfsw_epoll_wait(int epfd, struct epoll_event *events, int maxevents,
  1319. int timeout, dfsan_label epfd_label,
  1320. dfsan_label events_label, dfsan_label maxevents_label,
  1321. dfsan_label timeout_label, dfsan_label *ret_label) {
  1322. int ret = epoll_wait(epfd, events, maxevents, timeout);
  1323. if (ret > 0)
  1324. dfsan_set_label(0, events, ret * sizeof(*events));
  1325. *ret_label = 0;
  1326. return ret;
  1327. }
  1328. SANITIZER_INTERFACE_ATTRIBUTE
  1329. int __dfso_epoll_wait(int epfd, struct epoll_event *events, int maxevents,
  1330. int timeout, dfsan_label epfd_label,
  1331. dfsan_label events_label, dfsan_label maxevents_label,
  1332. dfsan_label timeout_label, dfsan_label *ret_label,
  1333. dfsan_origin epfd_origin, dfsan_origin events_origin,
  1334. dfsan_origin maxevents_origin,
  1335. dfsan_origin timeout_origin, dfsan_origin *ret_origin) {
  1336. return __dfsw_epoll_wait(epfd, events, maxevents, timeout, epfd_label,
  1337. events_label, maxevents_label, timeout_label,
  1338. ret_label);
  1339. }
  1340. SANITIZER_INTERFACE_ATTRIBUTE
  1341. int __dfsw_poll(struct pollfd *fds, nfds_t nfds, int timeout,
  1342. dfsan_label dfs_label, dfsan_label nfds_label,
  1343. dfsan_label timeout_label, dfsan_label *ret_label) {
  1344. int ret = poll(fds, nfds, timeout);
  1345. if (ret >= 0) {
  1346. for (; nfds > 0; --nfds) {
  1347. dfsan_set_label(0, &fds[nfds - 1].revents, sizeof(fds[nfds - 1].revents));
  1348. }
  1349. }
  1350. *ret_label = 0;
  1351. return ret;
  1352. }
  1353. SANITIZER_INTERFACE_ATTRIBUTE
  1354. int __dfso_poll(struct pollfd *fds, nfds_t nfds, int timeout,
  1355. dfsan_label dfs_label, dfsan_label nfds_label,
  1356. dfsan_label timeout_label, dfsan_label *ret_label,
  1357. dfsan_origin dfs_origin, dfsan_origin nfds_origin,
  1358. dfsan_origin timeout_origin, dfsan_origin *ret_origin) {
  1359. return __dfsw_poll(fds, nfds, timeout, dfs_label, nfds_label, timeout_label,
  1360. ret_label);
  1361. }
  1362. SANITIZER_INTERFACE_ATTRIBUTE
  1363. int __dfsw_select(int nfds, fd_set *readfds, fd_set *writefds,
  1364. fd_set *exceptfds, struct timeval *timeout,
  1365. dfsan_label nfds_label, dfsan_label readfds_label,
  1366. dfsan_label writefds_label, dfsan_label exceptfds_label,
  1367. dfsan_label timeout_label, dfsan_label *ret_label) {
  1368. int ret = select(nfds, readfds, writefds, exceptfds, timeout);
  1369. // Clear everything (also on error) since their content is either set or
  1370. // undefined.
  1371. if (readfds) {
  1372. dfsan_set_label(0, readfds, sizeof(fd_set));
  1373. }
  1374. if (writefds) {
  1375. dfsan_set_label(0, writefds, sizeof(fd_set));
  1376. }
  1377. if (exceptfds) {
  1378. dfsan_set_label(0, exceptfds, sizeof(fd_set));
  1379. }
  1380. dfsan_set_label(0, timeout, sizeof(struct timeval));
  1381. *ret_label = 0;
  1382. return ret;
  1383. }
  1384. SANITIZER_INTERFACE_ATTRIBUTE
  1385. int __dfso_select(int nfds, fd_set *readfds, fd_set *writefds,
  1386. fd_set *exceptfds, struct timeval *timeout,
  1387. dfsan_label nfds_label, dfsan_label readfds_label,
  1388. dfsan_label writefds_label, dfsan_label exceptfds_label,
  1389. dfsan_label timeout_label, dfsan_label *ret_label,
  1390. dfsan_origin nfds_origin, dfsan_origin readfds_origin,
  1391. dfsan_origin writefds_origin, dfsan_origin exceptfds_origin,
  1392. dfsan_origin timeout_origin, dfsan_origin *ret_origin) {
  1393. return __dfsw_select(nfds, readfds, writefds, exceptfds, timeout, nfds_label,
  1394. readfds_label, writefds_label, exceptfds_label,
  1395. timeout_label, ret_label);
  1396. }
  1397. SANITIZER_INTERFACE_ATTRIBUTE
  1398. int __dfsw_sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask,
  1399. dfsan_label pid_label,
  1400. dfsan_label cpusetsize_label,
  1401. dfsan_label mask_label, dfsan_label *ret_label) {
  1402. int ret = sched_getaffinity(pid, cpusetsize, mask);
  1403. if (ret == 0) {
  1404. dfsan_set_label(0, mask, cpusetsize);
  1405. }
  1406. *ret_label = 0;
  1407. return ret;
  1408. }
  1409. SANITIZER_INTERFACE_ATTRIBUTE
  1410. int __dfso_sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask,
  1411. dfsan_label pid_label,
  1412. dfsan_label cpusetsize_label,
  1413. dfsan_label mask_label, dfsan_label *ret_label,
  1414. dfsan_origin pid_origin,
  1415. dfsan_origin cpusetsize_origin,
  1416. dfsan_origin mask_origin,
  1417. dfsan_origin *ret_origin) {
  1418. return __dfsw_sched_getaffinity(pid, cpusetsize, mask, pid_label,
  1419. cpusetsize_label, mask_label, ret_label);
  1420. }
  1421. SANITIZER_INTERFACE_ATTRIBUTE
  1422. int __dfsw_sigemptyset(sigset_t *set, dfsan_label set_label,
  1423. dfsan_label *ret_label) {
  1424. int ret = sigemptyset(set);
  1425. dfsan_set_label(0, set, sizeof(sigset_t));
  1426. *ret_label = 0;
  1427. return ret;
  1428. }
  1429. SANITIZER_INTERFACE_ATTRIBUTE
  1430. int __dfso_sigemptyset(sigset_t *set, dfsan_label set_label,
  1431. dfsan_label *ret_label, dfsan_origin set_origin,
  1432. dfsan_origin *ret_origin) {
  1433. return __dfsw_sigemptyset(set, set_label, ret_label);
  1434. }
  1435. class SignalHandlerScope {
  1436. public:
  1437. SignalHandlerScope() {
  1438. if (DFsanThread *t = GetCurrentThread())
  1439. t->EnterSignalHandler();
  1440. }
  1441. ~SignalHandlerScope() {
  1442. if (DFsanThread *t = GetCurrentThread())
  1443. t->LeaveSignalHandler();
  1444. }
  1445. };
  1446. // Clear DFSan runtime TLS state at the end of a scope.
  1447. //
  1448. // Implementation must be async-signal-safe and use small data size, because
  1449. // instances of this class may live on the signal handler stack.
  1450. //
  1451. // DFSan uses TLS to pass metadata of arguments and return values. When an
  1452. // instrumented function accesses the TLS, if a signal callback happens, and the
  1453. // callback calls other instrumented functions with updating the same TLS, the
  1454. // TLS is in an inconsistent state after the callback ends. This may cause
  1455. // either under-tainting or over-tainting.
  1456. //
  1457. // The current implementation simply resets TLS at restore. This prevents from
  1458. // over-tainting. Although under-tainting may still happen, a taint flow can be
  1459. // found eventually if we run a DFSan-instrumented program multiple times. The
  1460. // alternative option is saving the entire TLS. However the TLS storage takes
  1461. // 2k bytes, and signal calls could be nested. So it does not seem worth.
  1462. class ScopedClearThreadLocalState {
  1463. public:
  1464. ScopedClearThreadLocalState() {}
  1465. ~ScopedClearThreadLocalState() { dfsan_clear_thread_local_state(); }
  1466. };
  1467. // SignalSpinLocker::sigactions_mu guarantees atomicity of sigaction() calls.
  1468. const int kMaxSignals = 1024;
  1469. static atomic_uintptr_t sigactions[kMaxSignals];
  1470. static void SignalHandler(int signo) {
  1471. SignalHandlerScope signal_handler_scope;
  1472. ScopedClearThreadLocalState scoped_clear_tls;
  1473. // Clear shadows for all inputs provided by system. This is why DFSan
  1474. // instrumentation generates a trampoline function to each function pointer,
  1475. // and uses the trampoline to clear shadows. However sigaction does not use
  1476. // a function pointer directly, so we have to do this manually.
  1477. dfsan_clear_arg_tls(0, sizeof(dfsan_label));
  1478. typedef void (*signal_cb)(int x);
  1479. signal_cb cb =
  1480. (signal_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
  1481. cb(signo);
  1482. }
  1483. static void SignalAction(int signo, siginfo_t *si, void *uc) {
  1484. SignalHandlerScope signal_handler_scope;
  1485. ScopedClearThreadLocalState scoped_clear_tls;
  1486. // Clear shadows for all inputs provided by system. Similar to SignalHandler.
  1487. dfsan_clear_arg_tls(0, 3 * sizeof(dfsan_label));
  1488. dfsan_set_label(0, si, sizeof(*si));
  1489. dfsan_set_label(0, uc, sizeof(ucontext_t));
  1490. typedef void (*sigaction_cb)(int, siginfo_t *, void *);
  1491. sigaction_cb cb =
  1492. (sigaction_cb)atomic_load(&sigactions[signo], memory_order_relaxed);
  1493. cb(signo, si, uc);
  1494. }
  1495. SANITIZER_INTERFACE_ATTRIBUTE
  1496. int __dfsw_sigaction(int signum, const struct sigaction *act,
  1497. struct sigaction *oldact, dfsan_label signum_label,
  1498. dfsan_label act_label, dfsan_label oldact_label,
  1499. dfsan_label *ret_label) {
  1500. CHECK_LT(signum, kMaxSignals);
  1501. SignalSpinLocker lock;
  1502. uptr old_cb = atomic_load(&sigactions[signum], memory_order_relaxed);
  1503. struct sigaction new_act;
  1504. struct sigaction *pnew_act = act ? &new_act : nullptr;
  1505. if (act) {
  1506. internal_memcpy(pnew_act, act, sizeof(struct sigaction));
  1507. if (pnew_act->sa_flags & SA_SIGINFO) {
  1508. uptr cb = (uptr)(pnew_act->sa_sigaction);
  1509. if (cb != (uptr)SIG_IGN && cb != (uptr)SIG_DFL) {
  1510. atomic_store(&sigactions[signum], cb, memory_order_relaxed);
  1511. pnew_act->sa_sigaction = SignalAction;
  1512. }
  1513. } else {
  1514. uptr cb = (uptr)(pnew_act->sa_handler);
  1515. if (cb != (uptr)SIG_IGN && cb != (uptr)SIG_DFL) {
  1516. atomic_store(&sigactions[signum], cb, memory_order_relaxed);
  1517. pnew_act->sa_handler = SignalHandler;
  1518. }
  1519. }
  1520. }
  1521. int ret = sigaction(signum, pnew_act, oldact);
  1522. if (ret == 0 && oldact) {
  1523. if (oldact->sa_flags & SA_SIGINFO) {
  1524. if (oldact->sa_sigaction == SignalAction)
  1525. oldact->sa_sigaction = (decltype(oldact->sa_sigaction))old_cb;
  1526. } else {
  1527. if (oldact->sa_handler == SignalHandler)
  1528. oldact->sa_handler = (decltype(oldact->sa_handler))old_cb;
  1529. }
  1530. }
  1531. if (oldact) {
  1532. dfsan_set_label(0, oldact, sizeof(struct sigaction));
  1533. }
  1534. *ret_label = 0;
  1535. return ret;
  1536. }
  1537. SANITIZER_INTERFACE_ATTRIBUTE
  1538. int __dfso_sigaction(int signum, const struct sigaction *act,
  1539. struct sigaction *oldact, dfsan_label signum_label,
  1540. dfsan_label act_label, dfsan_label oldact_label,
  1541. dfsan_label *ret_label, dfsan_origin signum_origin,
  1542. dfsan_origin act_origin, dfsan_origin oldact_origin,
  1543. dfsan_origin *ret_origin) {
  1544. return __dfsw_sigaction(signum, act, oldact, signum_label, act_label,
  1545. oldact_label, ret_label);
  1546. }
  1547. static sighandler_t dfsan_signal(int signum, sighandler_t handler,
  1548. dfsan_label *ret_label) {
  1549. CHECK_LT(signum, kMaxSignals);
  1550. SignalSpinLocker lock;
  1551. uptr old_cb = atomic_load(&sigactions[signum], memory_order_relaxed);
  1552. if (handler != SIG_IGN && handler != SIG_DFL) {
  1553. atomic_store(&sigactions[signum], (uptr)handler, memory_order_relaxed);
  1554. handler = &SignalHandler;
  1555. }
  1556. sighandler_t ret = signal(signum, handler);
  1557. if (ret == SignalHandler)
  1558. ret = (sighandler_t)old_cb;
  1559. *ret_label = 0;
  1560. return ret;
  1561. }
  1562. SANITIZER_INTERFACE_ATTRIBUTE
  1563. sighandler_t __dfsw_signal(int signum,
  1564. void *(*handler_trampoline)(void *, int, dfsan_label,
  1565. dfsan_label *),
  1566. sighandler_t handler, dfsan_label signum_label,
  1567. dfsan_label handler_label, dfsan_label *ret_label) {
  1568. return dfsan_signal(signum, handler, ret_label);
  1569. }
  1570. SANITIZER_INTERFACE_ATTRIBUTE
  1571. sighandler_t __dfso_signal(
  1572. int signum,
  1573. void *(*handler_trampoline)(void *, int, dfsan_label, dfsan_label *,
  1574. dfsan_origin, dfsan_origin *),
  1575. sighandler_t handler, dfsan_label signum_label, dfsan_label handler_label,
  1576. dfsan_label *ret_label, dfsan_origin signum_origin,
  1577. dfsan_origin handler_origin, dfsan_origin *ret_origin) {
  1578. return dfsan_signal(signum, handler, ret_label);
  1579. }
  1580. SANITIZER_INTERFACE_ATTRIBUTE
  1581. int __dfsw_sigaltstack(const stack_t *ss, stack_t *old_ss, dfsan_label ss_label,
  1582. dfsan_label old_ss_label, dfsan_label *ret_label) {
  1583. int ret = sigaltstack(ss, old_ss);
  1584. if (ret != -1 && old_ss)
  1585. dfsan_set_label(0, old_ss, sizeof(*old_ss));
  1586. *ret_label = 0;
  1587. return ret;
  1588. }
  1589. SANITIZER_INTERFACE_ATTRIBUTE
  1590. int __dfso_sigaltstack(const stack_t *ss, stack_t *old_ss, dfsan_label ss_label,
  1591. dfsan_label old_ss_label, dfsan_label *ret_label,
  1592. dfsan_origin ss_origin, dfsan_origin old_ss_origin,
  1593. dfsan_origin *ret_origin) {
  1594. return __dfsw_sigaltstack(ss, old_ss, ss_label, old_ss_label, ret_label);
  1595. }
  1596. SANITIZER_INTERFACE_ATTRIBUTE
  1597. int __dfsw_gettimeofday(struct timeval *tv, struct timezone *tz,
  1598. dfsan_label tv_label, dfsan_label tz_label,
  1599. dfsan_label *ret_label) {
  1600. int ret = gettimeofday(tv, tz);
  1601. if (tv) {
  1602. dfsan_set_label(0, tv, sizeof(struct timeval));
  1603. }
  1604. if (tz) {
  1605. dfsan_set_label(0, tz, sizeof(struct timezone));
  1606. }
  1607. *ret_label = 0;
  1608. return ret;
  1609. }
  1610. SANITIZER_INTERFACE_ATTRIBUTE
  1611. int __dfso_gettimeofday(struct timeval *tv, struct timezone *tz,
  1612. dfsan_label tv_label, dfsan_label tz_label,
  1613. dfsan_label *ret_label, dfsan_origin tv_origin,
  1614. dfsan_origin tz_origin, dfsan_origin *ret_origin) {
  1615. return __dfsw_gettimeofday(tv, tz, tv_label, tz_label, ret_label);
  1616. }
  1617. SANITIZER_INTERFACE_ATTRIBUTE void *__dfsw_memchr(void *s, int c, size_t n,
  1618. dfsan_label s_label,
  1619. dfsan_label c_label,
  1620. dfsan_label n_label,
  1621. dfsan_label *ret_label) {
  1622. void *ret = memchr(s, c, n);
  1623. if (flags().strict_data_dependencies) {
  1624. *ret_label = ret ? s_label : 0;
  1625. } else {
  1626. size_t len =
  1627. ret ? reinterpret_cast<char *>(ret) - reinterpret_cast<char *>(s) + 1
  1628. : n;
  1629. *ret_label =
  1630. dfsan_union(dfsan_read_label(s, len), dfsan_union(s_label, c_label));
  1631. }
  1632. return ret;
  1633. }
  1634. SANITIZER_INTERFACE_ATTRIBUTE void *__dfso_memchr(
  1635. void *s, int c, size_t n, dfsan_label s_label, dfsan_label c_label,
  1636. dfsan_label n_label, dfsan_label *ret_label, dfsan_origin s_origin,
  1637. dfsan_origin c_origin, dfsan_origin n_origin, dfsan_origin *ret_origin) {
  1638. void *ret = __dfsw_memchr(s, c, n, s_label, c_label, n_label, ret_label);
  1639. if (flags().strict_data_dependencies) {
  1640. if (ret)
  1641. *ret_origin = s_origin;
  1642. } else {
  1643. size_t len =
  1644. ret ? reinterpret_cast<char *>(ret) - reinterpret_cast<char *>(s) + 1
  1645. : n;
  1646. dfsan_origin o = dfsan_read_origin_of_first_taint(s, len);
  1647. *ret_origin = o ? o : (s_label ? s_origin : c_origin);
  1648. }
  1649. return ret;
  1650. }
  1651. SANITIZER_INTERFACE_ATTRIBUTE char *__dfsw_strrchr(char *s, int c,
  1652. dfsan_label s_label,
  1653. dfsan_label c_label,
  1654. dfsan_label *ret_label) {
  1655. char *ret = strrchr(s, c);
  1656. if (flags().strict_data_dependencies) {
  1657. *ret_label = ret ? s_label : 0;
  1658. } else {
  1659. *ret_label =
  1660. dfsan_union(dfsan_read_label(s, strlen(s) + 1),
  1661. dfsan_union(s_label, c_label));
  1662. }
  1663. return ret;
  1664. }
  1665. SANITIZER_INTERFACE_ATTRIBUTE char *__dfso_strrchr(
  1666. char *s, int c, dfsan_label s_label, dfsan_label c_label,
  1667. dfsan_label *ret_label, dfsan_origin s_origin, dfsan_origin c_origin,
  1668. dfsan_origin *ret_origin) {
  1669. char *ret = __dfsw_strrchr(s, c, s_label, c_label, ret_label);
  1670. if (flags().strict_data_dependencies) {
  1671. if (ret)
  1672. *ret_origin = s_origin;
  1673. } else {
  1674. size_t s_len = strlen(s) + 1;
  1675. dfsan_origin o = dfsan_read_origin_of_first_taint(s, s_len);
  1676. *ret_origin = o ? o : (s_label ? s_origin : c_origin);
  1677. }
  1678. return ret;
  1679. }
  1680. SANITIZER_INTERFACE_ATTRIBUTE char *__dfsw_strstr(char *haystack, char *needle,
  1681. dfsan_label haystack_label,
  1682. dfsan_label needle_label,
  1683. dfsan_label *ret_label) {
  1684. char *ret = strstr(haystack, needle);
  1685. if (flags().strict_data_dependencies) {
  1686. *ret_label = ret ? haystack_label : 0;
  1687. } else {
  1688. size_t len = ret ? ret + strlen(needle) - haystack : strlen(haystack) + 1;
  1689. *ret_label =
  1690. dfsan_union(dfsan_read_label(haystack, len),
  1691. dfsan_union(dfsan_read_label(needle, strlen(needle) + 1),
  1692. dfsan_union(haystack_label, needle_label)));
  1693. }
  1694. return ret;
  1695. }
  1696. SANITIZER_INTERFACE_ATTRIBUTE char *__dfso_strstr(char *haystack, char *needle,
  1697. dfsan_label haystack_label,
  1698. dfsan_label needle_label,
  1699. dfsan_label *ret_label,
  1700. dfsan_origin haystack_origin,
  1701. dfsan_origin needle_origin,
  1702. dfsan_origin *ret_origin) {
  1703. char *ret =
  1704. __dfsw_strstr(haystack, needle, haystack_label, needle_label, ret_label);
  1705. if (flags().strict_data_dependencies) {
  1706. if (ret)
  1707. *ret_origin = haystack_origin;
  1708. } else {
  1709. size_t needle_len = strlen(needle);
  1710. size_t len = ret ? ret + needle_len - haystack : strlen(haystack) + 1;
  1711. dfsan_origin o = dfsan_read_origin_of_first_taint(haystack, len);
  1712. if (o) {
  1713. *ret_origin = o;
  1714. } else {
  1715. o = dfsan_read_origin_of_first_taint(needle, needle_len + 1);
  1716. *ret_origin = o ? o : (haystack_label ? haystack_origin : needle_origin);
  1717. }
  1718. }
  1719. return ret;
  1720. }
  1721. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_nanosleep(const struct timespec *req,
  1722. struct timespec *rem,
  1723. dfsan_label req_label,
  1724. dfsan_label rem_label,
  1725. dfsan_label *ret_label) {
  1726. int ret = nanosleep(req, rem);
  1727. *ret_label = 0;
  1728. if (ret == -1) {
  1729. // Interrupted by a signal, rem is filled with the remaining time.
  1730. dfsan_set_label(0, rem, sizeof(struct timespec));
  1731. }
  1732. return ret;
  1733. }
  1734. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_nanosleep(
  1735. const struct timespec *req, struct timespec *rem, dfsan_label req_label,
  1736. dfsan_label rem_label, dfsan_label *ret_label, dfsan_origin req_origin,
  1737. dfsan_origin rem_origin, dfsan_origin *ret_origin) {
  1738. return __dfsw_nanosleep(req, rem, req_label, rem_label, ret_label);
  1739. }
  1740. static void clear_msghdr_labels(size_t bytes_written, struct msghdr *msg) {
  1741. dfsan_set_label(0, msg, sizeof(*msg));
  1742. dfsan_set_label(0, msg->msg_name, msg->msg_namelen);
  1743. dfsan_set_label(0, msg->msg_control, msg->msg_controllen);
  1744. for (size_t i = 0; bytes_written > 0; ++i) {
  1745. assert(i < msg->msg_iovlen);
  1746. struct iovec *iov = &msg->msg_iov[i];
  1747. size_t iov_written =
  1748. bytes_written < iov->iov_len ? bytes_written : iov->iov_len;
  1749. dfsan_set_label(0, iov->iov_base, iov_written);
  1750. bytes_written -= iov_written;
  1751. }
  1752. }
  1753. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_recvmmsg(
  1754. int sockfd, struct mmsghdr *msgvec, unsigned int vlen, int flags,
  1755. struct timespec *timeout, dfsan_label sockfd_label,
  1756. dfsan_label msgvec_label, dfsan_label vlen_label, dfsan_label flags_label,
  1757. dfsan_label timeout_label, dfsan_label *ret_label) {
  1758. int ret = recvmmsg(sockfd, msgvec, vlen, flags, timeout);
  1759. for (int i = 0; i < ret; ++i) {
  1760. dfsan_set_label(0, &msgvec[i].msg_len, sizeof(msgvec[i].msg_len));
  1761. clear_msghdr_labels(msgvec[i].msg_len, &msgvec[i].msg_hdr);
  1762. }
  1763. *ret_label = 0;
  1764. return ret;
  1765. }
  1766. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_recvmmsg(
  1767. int sockfd, struct mmsghdr *msgvec, unsigned int vlen, int flags,
  1768. struct timespec *timeout, dfsan_label sockfd_label,
  1769. dfsan_label msgvec_label, dfsan_label vlen_label, dfsan_label flags_label,
  1770. dfsan_label timeout_label, dfsan_label *ret_label,
  1771. dfsan_origin sockfd_origin, dfsan_origin msgvec_origin,
  1772. dfsan_origin vlen_origin, dfsan_origin flags_origin,
  1773. dfsan_origin timeout_origin, dfsan_origin *ret_origin) {
  1774. return __dfsw_recvmmsg(sockfd, msgvec, vlen, flags, timeout, sockfd_label,
  1775. msgvec_label, vlen_label, flags_label, timeout_label,
  1776. ret_label);
  1777. }
  1778. SANITIZER_INTERFACE_ATTRIBUTE ssize_t __dfsw_recvmsg(
  1779. int sockfd, struct msghdr *msg, int flags, dfsan_label sockfd_label,
  1780. dfsan_label msg_label, dfsan_label flags_label, dfsan_label *ret_label) {
  1781. ssize_t ret = recvmsg(sockfd, msg, flags);
  1782. if (ret >= 0)
  1783. clear_msghdr_labels(ret, msg);
  1784. *ret_label = 0;
  1785. return ret;
  1786. }
  1787. SANITIZER_INTERFACE_ATTRIBUTE ssize_t __dfso_recvmsg(
  1788. int sockfd, struct msghdr *msg, int flags, dfsan_label sockfd_label,
  1789. dfsan_label msg_label, dfsan_label flags_label, dfsan_label *ret_label,
  1790. dfsan_origin sockfd_origin, dfsan_origin msg_origin,
  1791. dfsan_origin flags_origin, dfsan_origin *ret_origin) {
  1792. return __dfsw_recvmsg(sockfd, msg, flags, sockfd_label, msg_label,
  1793. flags_label, ret_label);
  1794. }
  1795. SANITIZER_INTERFACE_ATTRIBUTE int
  1796. __dfsw_socketpair(int domain, int type, int protocol, int sv[2],
  1797. dfsan_label domain_label, dfsan_label type_label,
  1798. dfsan_label protocol_label, dfsan_label sv_label,
  1799. dfsan_label *ret_label) {
  1800. int ret = socketpair(domain, type, protocol, sv);
  1801. *ret_label = 0;
  1802. if (ret == 0) {
  1803. dfsan_set_label(0, sv, sizeof(*sv) * 2);
  1804. }
  1805. return ret;
  1806. }
  1807. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_socketpair(
  1808. int domain, int type, int protocol, int sv[2], dfsan_label domain_label,
  1809. dfsan_label type_label, dfsan_label protocol_label, dfsan_label sv_label,
  1810. dfsan_label *ret_label, dfsan_origin domain_origin,
  1811. dfsan_origin type_origin, dfsan_origin protocol_origin,
  1812. dfsan_origin sv_origin, dfsan_origin *ret_origin) {
  1813. return __dfsw_socketpair(domain, type, protocol, sv, domain_label, type_label,
  1814. protocol_label, sv_label, ret_label);
  1815. }
  1816. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_getsockopt(
  1817. int sockfd, int level, int optname, void *optval, socklen_t *optlen,
  1818. dfsan_label sockfd_label, dfsan_label level_label,
  1819. dfsan_label optname_label, dfsan_label optval_label,
  1820. dfsan_label optlen_label, dfsan_label *ret_label) {
  1821. int ret = getsockopt(sockfd, level, optname, optval, optlen);
  1822. if (ret != -1 && optval && optlen) {
  1823. dfsan_set_label(0, optlen, sizeof(*optlen));
  1824. dfsan_set_label(0, optval, *optlen);
  1825. }
  1826. *ret_label = 0;
  1827. return ret;
  1828. }
  1829. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_getsockopt(
  1830. int sockfd, int level, int optname, void *optval, socklen_t *optlen,
  1831. dfsan_label sockfd_label, dfsan_label level_label,
  1832. dfsan_label optname_label, dfsan_label optval_label,
  1833. dfsan_label optlen_label, dfsan_label *ret_label,
  1834. dfsan_origin sockfd_origin, dfsan_origin level_origin,
  1835. dfsan_origin optname_origin, dfsan_origin optval_origin,
  1836. dfsan_origin optlen_origin, dfsan_origin *ret_origin) {
  1837. return __dfsw_getsockopt(sockfd, level, optname, optval, optlen, sockfd_label,
  1838. level_label, optname_label, optval_label,
  1839. optlen_label, ret_label);
  1840. }
  1841. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_getsockname(
  1842. int sockfd, struct sockaddr *addr, socklen_t *addrlen,
  1843. dfsan_label sockfd_label, dfsan_label addr_label, dfsan_label addrlen_label,
  1844. dfsan_label *ret_label) {
  1845. socklen_t origlen = addrlen ? *addrlen : 0;
  1846. int ret = getsockname(sockfd, addr, addrlen);
  1847. if (ret != -1 && addr && addrlen) {
  1848. socklen_t written_bytes = origlen < *addrlen ? origlen : *addrlen;
  1849. dfsan_set_label(0, addrlen, sizeof(*addrlen));
  1850. dfsan_set_label(0, addr, written_bytes);
  1851. }
  1852. *ret_label = 0;
  1853. return ret;
  1854. }
  1855. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_getsockname(
  1856. int sockfd, struct sockaddr *addr, socklen_t *addrlen,
  1857. dfsan_label sockfd_label, dfsan_label addr_label, dfsan_label addrlen_label,
  1858. dfsan_label *ret_label, dfsan_origin sockfd_origin,
  1859. dfsan_origin addr_origin, dfsan_origin addrlen_origin,
  1860. dfsan_origin *ret_origin) {
  1861. return __dfsw_getsockname(sockfd, addr, addrlen, sockfd_label, addr_label,
  1862. addrlen_label, ret_label);
  1863. }
  1864. SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_getpeername(
  1865. int sockfd, struct sockaddr *addr, socklen_t *addrlen,
  1866. dfsan_label sockfd_label, dfsan_label addr_label, dfsan_label addrlen_label,
  1867. dfsan_label *ret_label) {
  1868. socklen_t origlen = addrlen ? *addrlen : 0;
  1869. int ret = getpeername(sockfd, addr, addrlen);
  1870. if (ret != -1 && addr && addrlen) {
  1871. socklen_t written_bytes = origlen < *addrlen ? origlen : *addrlen;
  1872. dfsan_set_label(0, addrlen, sizeof(*addrlen));
  1873. dfsan_set_label(0, addr, written_bytes);
  1874. }
  1875. *ret_label = 0;
  1876. return ret;
  1877. }
  1878. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_getpeername(
  1879. int sockfd, struct sockaddr *addr, socklen_t *addrlen,
  1880. dfsan_label sockfd_label, dfsan_label addr_label, dfsan_label addrlen_label,
  1881. dfsan_label *ret_label, dfsan_origin sockfd_origin,
  1882. dfsan_origin addr_origin, dfsan_origin addrlen_origin,
  1883. dfsan_origin *ret_origin) {
  1884. return __dfsw_getpeername(sockfd, addr, addrlen, sockfd_label, addr_label,
  1885. addrlen_label, ret_label);
  1886. }
  1887. // Type of the trampoline function passed to the custom version of
  1888. // dfsan_set_write_callback.
  1889. typedef void (*write_trampoline_t)(
  1890. void *callback,
  1891. int fd, const void *buf, ssize_t count,
  1892. dfsan_label fd_label, dfsan_label buf_label, dfsan_label count_label);
  1893. typedef void (*write_origin_trampoline_t)(
  1894. void *callback, int fd, const void *buf, ssize_t count,
  1895. dfsan_label fd_label, dfsan_label buf_label, dfsan_label count_label,
  1896. dfsan_origin fd_origin, dfsan_origin buf_origin, dfsan_origin count_origin);
  1897. // Calls to dfsan_set_write_callback() set the values in this struct.
  1898. // Calls to the custom version of write() read (and invoke) them.
  1899. static struct {
  1900. write_trampoline_t write_callback_trampoline = nullptr;
  1901. void *write_callback = nullptr;
  1902. } write_callback_info;
  1903. static struct {
  1904. write_origin_trampoline_t write_callback_trampoline = nullptr;
  1905. void *write_callback = nullptr;
  1906. } write_origin_callback_info;
  1907. SANITIZER_INTERFACE_ATTRIBUTE void
  1908. __dfsw_dfsan_set_write_callback(
  1909. write_trampoline_t write_callback_trampoline,
  1910. void *write_callback,
  1911. dfsan_label write_callback_label,
  1912. dfsan_label *ret_label) {
  1913. write_callback_info.write_callback_trampoline = write_callback_trampoline;
  1914. write_callback_info.write_callback = write_callback;
  1915. }
  1916. SANITIZER_INTERFACE_ATTRIBUTE void __dfso_dfsan_set_write_callback(
  1917. write_origin_trampoline_t write_callback_trampoline, void *write_callback,
  1918. dfsan_label write_callback_label, dfsan_label *ret_label,
  1919. dfsan_origin write_callback_origin, dfsan_origin *ret_origin) {
  1920. write_origin_callback_info.write_callback_trampoline =
  1921. write_callback_trampoline;
  1922. write_origin_callback_info.write_callback = write_callback;
  1923. }
  1924. SANITIZER_INTERFACE_ATTRIBUTE int
  1925. __dfsw_write(int fd, const void *buf, size_t count,
  1926. dfsan_label fd_label, dfsan_label buf_label,
  1927. dfsan_label count_label, dfsan_label *ret_label) {
  1928. if (write_callback_info.write_callback) {
  1929. write_callback_info.write_callback_trampoline(
  1930. write_callback_info.write_callback,
  1931. fd, buf, count,
  1932. fd_label, buf_label, count_label);
  1933. }
  1934. *ret_label = 0;
  1935. return write(fd, buf, count);
  1936. }
  1937. SANITIZER_INTERFACE_ATTRIBUTE int __dfso_write(
  1938. int fd, const void *buf, size_t count, dfsan_label fd_label,
  1939. dfsan_label buf_label, dfsan_label count_label, dfsan_label *ret_label,
  1940. dfsan_origin fd_origin, dfsan_origin buf_origin, dfsan_origin count_origin,
  1941. dfsan_origin *ret_origin) {
  1942. if (write_origin_callback_info.write_callback) {
  1943. write_origin_callback_info.write_callback_trampoline(
  1944. write_origin_callback_info.write_callback, fd, buf, count, fd_label,
  1945. buf_label, count_label, fd_origin, buf_origin, count_origin);
  1946. }
  1947. *ret_label = 0;
  1948. return write(fd, buf, count);
  1949. }
  1950. } // namespace __dfsan
  1951. // Type used to extract a dfsan_label with va_arg()
  1952. typedef int dfsan_label_va;
  1953. // Formats a chunk either a constant string or a single format directive (e.g.,
  1954. // '%.3f').
  1955. struct Formatter {
  1956. Formatter(char *str_, const char *fmt_, size_t size_)
  1957. : str(str_), str_off(0), size(size_), fmt_start(fmt_), fmt_cur(fmt_),
  1958. width(-1) {}
  1959. int format() {
  1960. char *tmp_fmt = build_format_string();
  1961. int retval =
  1962. snprintf(str + str_off, str_off < size ? size - str_off : 0, tmp_fmt,
  1963. 0 /* used only to avoid warnings */);
  1964. free(tmp_fmt);
  1965. return retval;
  1966. }
  1967. template <typename T> int format(T arg) {
  1968. char *tmp_fmt = build_format_string();
  1969. int retval;
  1970. if (width >= 0) {
  1971. retval = snprintf(str + str_off, str_off < size ? size - str_off : 0,
  1972. tmp_fmt, width, arg);
  1973. } else {
  1974. retval = snprintf(str + str_off, str_off < size ? size - str_off : 0,
  1975. tmp_fmt, arg);
  1976. }
  1977. free(tmp_fmt);
  1978. return retval;
  1979. }
  1980. char *build_format_string() {
  1981. size_t fmt_size = fmt_cur - fmt_start + 1;
  1982. char *new_fmt = (char *)malloc(fmt_size + 1);
  1983. assert(new_fmt);
  1984. internal_memcpy(new_fmt, fmt_start, fmt_size);
  1985. new_fmt[fmt_size] = '\0';
  1986. return new_fmt;
  1987. }
  1988. char *str_cur() { return str + str_off; }
  1989. size_t num_written_bytes(int retval) {
  1990. if (retval < 0) {
  1991. return 0;
  1992. }
  1993. size_t num_avail = str_off < size ? size - str_off : 0;
  1994. if (num_avail == 0) {
  1995. return 0;
  1996. }
  1997. size_t num_written = retval;
  1998. // A return value of {v,}snprintf of size or more means that the output was
  1999. // truncated.
  2000. if (num_written >= num_avail) {
  2001. num_written -= num_avail;
  2002. }
  2003. return num_written;
  2004. }
  2005. char *str;
  2006. size_t str_off;
  2007. size_t size;
  2008. const char *fmt_start;
  2009. const char *fmt_cur;
  2010. int width;
  2011. };
  2012. // Formats the input and propagates the input labels to the output. The output
  2013. // is stored in 'str'. 'size' bounds the number of output bytes. 'format' and
  2014. // 'ap' are the format string and the list of arguments for formatting. Returns
  2015. // the return value vsnprintf would return.
  2016. //
  2017. // The function tokenizes the format string in chunks representing either a
  2018. // constant string or a single format directive (e.g., '%.3f') and formats each
  2019. // chunk independently into the output string. This approach allows to figure
  2020. // out which bytes of the output string depends on which argument and thus to
  2021. // propagate labels more precisely.
  2022. //
  2023. // WARNING: This implementation does not support conversion specifiers with
  2024. // positional arguments.
  2025. static int format_buffer(char *str, size_t size, const char *fmt,
  2026. dfsan_label *va_labels, dfsan_label *ret_label,
  2027. dfsan_origin *va_origins, dfsan_origin *ret_origin,
  2028. va_list ap) {
  2029. Formatter formatter(str, fmt, size);
  2030. while (*formatter.fmt_cur) {
  2031. formatter.fmt_start = formatter.fmt_cur;
  2032. formatter.width = -1;
  2033. int retval = 0;
  2034. if (*formatter.fmt_cur != '%') {
  2035. // Ordinary character. Consume all the characters until a '%' or the end
  2036. // of the string.
  2037. for (; *(formatter.fmt_cur + 1) && *(formatter.fmt_cur + 1) != '%';
  2038. ++formatter.fmt_cur) {}
  2039. retval = formatter.format();
  2040. dfsan_set_label(0, formatter.str_cur(),
  2041. formatter.num_written_bytes(retval));
  2042. } else {
  2043. // Conversion directive. Consume all the characters until a conversion
  2044. // specifier or the end of the string.
  2045. bool end_fmt = false;
  2046. for (; *formatter.fmt_cur && !end_fmt; ) {
  2047. switch (*++formatter.fmt_cur) {
  2048. case 'd':
  2049. case 'i':
  2050. case 'o':
  2051. case 'u':
  2052. case 'x':
  2053. case 'X':
  2054. switch (*(formatter.fmt_cur - 1)) {
  2055. case 'h':
  2056. // Also covers the 'hh' case (since the size of the arg is still
  2057. // an int).
  2058. retval = formatter.format(va_arg(ap, int));
  2059. break;
  2060. case 'l':
  2061. if (formatter.fmt_cur - formatter.fmt_start >= 2 &&
  2062. *(formatter.fmt_cur - 2) == 'l') {
  2063. retval = formatter.format(va_arg(ap, long long int));
  2064. } else {
  2065. retval = formatter.format(va_arg(ap, long int));
  2066. }
  2067. break;
  2068. case 'q':
  2069. retval = formatter.format(va_arg(ap, long long int));
  2070. break;
  2071. case 'j':
  2072. retval = formatter.format(va_arg(ap, intmax_t));
  2073. break;
  2074. case 'z':
  2075. case 't':
  2076. retval = formatter.format(va_arg(ap, size_t));
  2077. break;
  2078. default:
  2079. retval = formatter.format(va_arg(ap, int));
  2080. }
  2081. if (va_origins == nullptr)
  2082. dfsan_set_label(*va_labels++, formatter.str_cur(),
  2083. formatter.num_written_bytes(retval));
  2084. else
  2085. dfsan_set_label_origin(*va_labels++, *va_origins++,
  2086. formatter.str_cur(),
  2087. formatter.num_written_bytes(retval));
  2088. end_fmt = true;
  2089. break;
  2090. case 'a':
  2091. case 'A':
  2092. case 'e':
  2093. case 'E':
  2094. case 'f':
  2095. case 'F':
  2096. case 'g':
  2097. case 'G':
  2098. if (*(formatter.fmt_cur - 1) == 'L') {
  2099. retval = formatter.format(va_arg(ap, long double));
  2100. } else {
  2101. retval = formatter.format(va_arg(ap, double));
  2102. }
  2103. if (va_origins == nullptr)
  2104. dfsan_set_label(*va_labels++, formatter.str_cur(),
  2105. formatter.num_written_bytes(retval));
  2106. else
  2107. dfsan_set_label_origin(*va_labels++, *va_origins++,
  2108. formatter.str_cur(),
  2109. formatter.num_written_bytes(retval));
  2110. end_fmt = true;
  2111. break;
  2112. case 'c':
  2113. retval = formatter.format(va_arg(ap, int));
  2114. if (va_origins == nullptr)
  2115. dfsan_set_label(*va_labels++, formatter.str_cur(),
  2116. formatter.num_written_bytes(retval));
  2117. else
  2118. dfsan_set_label_origin(*va_labels++, *va_origins++,
  2119. formatter.str_cur(),
  2120. formatter.num_written_bytes(retval));
  2121. end_fmt = true;
  2122. break;
  2123. case 's': {
  2124. char *arg = va_arg(ap, char *);
  2125. retval = formatter.format(arg);
  2126. if (va_origins) {
  2127. va_origins++;
  2128. dfsan_mem_origin_transfer(formatter.str_cur(), arg,
  2129. formatter.num_written_bytes(retval));
  2130. }
  2131. va_labels++;
  2132. dfsan_mem_shadow_transfer(formatter.str_cur(), arg,
  2133. formatter.num_written_bytes(retval));
  2134. end_fmt = true;
  2135. break;
  2136. }
  2137. case 'p':
  2138. retval = formatter.format(va_arg(ap, void *));
  2139. if (va_origins == nullptr)
  2140. dfsan_set_label(*va_labels++, formatter.str_cur(),
  2141. formatter.num_written_bytes(retval));
  2142. else
  2143. dfsan_set_label_origin(*va_labels++, *va_origins++,
  2144. formatter.str_cur(),
  2145. formatter.num_written_bytes(retval));
  2146. end_fmt = true;
  2147. break;
  2148. case 'n': {
  2149. int *ptr = va_arg(ap, int *);
  2150. *ptr = (int)formatter.str_off;
  2151. va_labels++;
  2152. if (va_origins)
  2153. va_origins++;
  2154. dfsan_set_label(0, ptr, sizeof(ptr));
  2155. end_fmt = true;
  2156. break;
  2157. }
  2158. case '%':
  2159. retval = formatter.format();
  2160. dfsan_set_label(0, formatter.str_cur(),
  2161. formatter.num_written_bytes(retval));
  2162. end_fmt = true;
  2163. break;
  2164. case '*':
  2165. formatter.width = va_arg(ap, int);
  2166. va_labels++;
  2167. if (va_origins)
  2168. va_origins++;
  2169. break;
  2170. default:
  2171. break;
  2172. }
  2173. }
  2174. }
  2175. if (retval < 0) {
  2176. return retval;
  2177. }
  2178. formatter.fmt_cur++;
  2179. formatter.str_off += retval;
  2180. }
  2181. *ret_label = 0;
  2182. if (ret_origin)
  2183. *ret_origin = 0;
  2184. // Number of bytes written in total.
  2185. return formatter.str_off;
  2186. }
  2187. extern "C" {
  2188. SANITIZER_INTERFACE_ATTRIBUTE
  2189. int __dfsw_sprintf(char *str, const char *format, dfsan_label str_label,
  2190. dfsan_label format_label, dfsan_label *va_labels,
  2191. dfsan_label *ret_label, ...) {
  2192. va_list ap;
  2193. va_start(ap, ret_label);
  2194. int ret = format_buffer(str, ~0ul, format, va_labels, ret_label, nullptr,
  2195. nullptr, ap);
  2196. va_end(ap);
  2197. return ret;
  2198. }
  2199. SANITIZER_INTERFACE_ATTRIBUTE
  2200. int __dfso_sprintf(char *str, const char *format, dfsan_label str_label,
  2201. dfsan_label format_label, dfsan_label *va_labels,
  2202. dfsan_label *ret_label, dfsan_origin str_origin,
  2203. dfsan_origin format_origin, dfsan_origin *va_origins,
  2204. dfsan_origin *ret_origin, ...) {
  2205. va_list ap;
  2206. va_start(ap, ret_origin);
  2207. int ret = format_buffer(str, ~0ul, format, va_labels, ret_label, va_origins,
  2208. ret_origin, ap);
  2209. va_end(ap);
  2210. return ret;
  2211. }
  2212. SANITIZER_INTERFACE_ATTRIBUTE
  2213. int __dfsw_snprintf(char *str, size_t size, const char *format,
  2214. dfsan_label str_label, dfsan_label size_label,
  2215. dfsan_label format_label, dfsan_label *va_labels,
  2216. dfsan_label *ret_label, ...) {
  2217. va_list ap;
  2218. va_start(ap, ret_label);
  2219. int ret = format_buffer(str, size, format, va_labels, ret_label, nullptr,
  2220. nullptr, ap);
  2221. va_end(ap);
  2222. return ret;
  2223. }
  2224. SANITIZER_INTERFACE_ATTRIBUTE
  2225. int __dfso_snprintf(char *str, size_t size, const char *format,
  2226. dfsan_label str_label, dfsan_label size_label,
  2227. dfsan_label format_label, dfsan_label *va_labels,
  2228. dfsan_label *ret_label, dfsan_origin str_origin,
  2229. dfsan_origin size_origin, dfsan_origin format_origin,
  2230. dfsan_origin *va_origins, dfsan_origin *ret_origin, ...) {
  2231. va_list ap;
  2232. va_start(ap, ret_origin);
  2233. int ret = format_buffer(str, size, format, va_labels, ret_label, va_origins,
  2234. ret_origin, ap);
  2235. va_end(ap);
  2236. return ret;
  2237. }
  2238. static void BeforeFork() {
  2239. StackDepotLockAll();
  2240. GetChainedOriginDepot()->LockAll();
  2241. }
  2242. static void AfterFork() {
  2243. GetChainedOriginDepot()->UnlockAll();
  2244. StackDepotUnlockAll();
  2245. }
  2246. SANITIZER_INTERFACE_ATTRIBUTE
  2247. pid_t __dfsw_fork(dfsan_label *ret_label) {
  2248. pid_t pid = fork();
  2249. *ret_label = 0;
  2250. return pid;
  2251. }
  2252. SANITIZER_INTERFACE_ATTRIBUTE
  2253. pid_t __dfso_fork(dfsan_label *ret_label, dfsan_origin *ret_origin) {
  2254. BeforeFork();
  2255. pid_t pid = __dfsw_fork(ret_label);
  2256. AfterFork();
  2257. return pid;
  2258. }
  2259. // Default empty implementations (weak). Users should redefine them.
  2260. SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_guard, u32 *) {}
  2261. SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_guard_init, u32 *,
  2262. u32 *) {}
  2263. SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_pcs_init, const uptr *beg,
  2264. const uptr *end) {}
  2265. SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_indir, void) {}
  2266. SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_cmp, void) {}
  2267. SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_cmp1, void) {}
  2268. SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_cmp2, void) {}
  2269. SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_cmp4, void) {}
  2270. SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_cmp8, void) {}
  2271. SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_const_cmp1,
  2272. void) {}
  2273. SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_const_cmp2,
  2274. void) {}
  2275. SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_const_cmp4,
  2276. void) {}
  2277. SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_const_cmp8,
  2278. void) {}
  2279. SANITIZER_INTERFACE_WEAK_DEF(void, __dfsw___sanitizer_cov_trace_switch, void) {}
  2280. } // extern "C"