1 //===-- sanitizer_common_interceptors_ioctl.inc -----------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Ioctl handling in common sanitizer interceptors.
9 //===----------------------------------------------------------------------===//
11 #include "sanitizer_flags.h"
15 // FIXME: support read+write arguments. Currently READWRITE and WRITE do the
17 // XXX: The declarations below may use WRITE instead of READWRITE, unless
30 const unsigned ioctl_table_max = 500;
31 static ioctl_desc ioctl_table[ioctl_table_max];
32 static unsigned ioctl_table_size = 0;
34 // This can not be declared as a global, because references to struct_*_sz
35 // require a global initializer. And this table must be available before global
36 // initializers are run.
37 static void ioctl_table_fill() {
38 #define _(rq, tp, sz) \
39 if (IOCTL_##rq != IOCTL_NOT_PRESENT) { \
40 CHECK(ioctl_table_size < ioctl_table_max); \
41 ioctl_table[ioctl_table_size].req = IOCTL_##rq; \
42 ioctl_table[ioctl_table_size].type = ioctl_desc::tp; \
43 ioctl_table[ioctl_table_size].size = sz; \
44 ioctl_table[ioctl_table_size].name = #rq; \
48 _(FIOASYNC, READ, sizeof(int));
50 _(FIOGETOWN, WRITE, sizeof(int));
51 _(FIONBIO, READ, sizeof(int));
53 _(FIOSETOWN, READ, sizeof(int));
54 _(SIOCATMARK, WRITE, sizeof(int));
55 _(SIOCGIFCONF, CUSTOM, 0);
56 _(SIOCGPGRP, WRITE, sizeof(int));
57 _(SIOCSPGRP, READ, sizeof(int));
60 _(TIOCGETD, WRITE, sizeof(int));
61 _(TIOCGPGRP, WRITE, pid_t_sz);
62 _(TIOCGWINSZ, WRITE, struct_winsize_sz);
63 _(TIOCMBIC, READ, sizeof(int));
64 _(TIOCMBIS, READ, sizeof(int));
65 _(TIOCMGET, WRITE, sizeof(int));
66 _(TIOCMSET, READ, sizeof(int));
67 _(TIOCNOTTY, NONE, 0);
69 _(TIOCOUTQ, WRITE, sizeof(int));
70 _(TIOCPKT, READ, sizeof(int));
71 _(TIOCSCTTY, NONE, 0);
72 _(TIOCSETD, READ, sizeof(int));
73 _(TIOCSPGRP, READ, pid_t_sz);
74 _(TIOCSTI, READ, sizeof(char));
75 _(TIOCSWINSZ, READ, struct_winsize_sz);
78 _(SIOCADDMULTI, READ, struct_ifreq_sz);
79 _(SIOCDELMULTI, READ, struct_ifreq_sz);
80 _(SIOCGIFADDR, WRITE, struct_ifreq_sz);
81 _(SIOCGIFBRDADDR, WRITE, struct_ifreq_sz);
82 _(SIOCGIFDSTADDR, WRITE, struct_ifreq_sz);
83 _(SIOCGIFFLAGS, WRITE, struct_ifreq_sz);
84 _(SIOCGIFMETRIC, WRITE, struct_ifreq_sz);
85 _(SIOCGIFMTU, WRITE, struct_ifreq_sz);
86 _(SIOCGIFNETMASK, WRITE, struct_ifreq_sz);
87 _(SIOCSIFADDR, READ, struct_ifreq_sz);
88 _(SIOCSIFBRDADDR, READ, struct_ifreq_sz);
89 _(SIOCSIFDSTADDR, READ, struct_ifreq_sz);
90 _(SIOCSIFFLAGS, READ, struct_ifreq_sz);
91 _(SIOCSIFMETRIC, READ, struct_ifreq_sz);
92 _(SIOCSIFMTU, READ, struct_ifreq_sz);
93 _(SIOCSIFNETMASK, READ, struct_ifreq_sz);
96 #if (SANITIZER_LINUX && !SANITIZER_ANDROID)
97 _(SIOCGETSGCNT, WRITE, struct_sioc_sg_req_sz);
98 _(SIOCGETVIFCNT, WRITE, struct_sioc_vif_req_sz);
102 // Conflicting request ids.
103 // _(CDROMAUDIOBUFSIZ, NONE, 0);
104 // _(SNDCTL_TMR_CONTINUE, NONE, 0);
105 // _(SNDCTL_TMR_START, NONE, 0);
106 // _(SNDCTL_TMR_STOP, NONE, 0);
107 // _(SOUND_MIXER_READ_LOUD, WRITE, sizeof(int)); // same as ...READ_ENHANCE
108 // _(SOUND_MIXER_READ_MUTE, WRITE, sizeof(int)); // same as ...READ_ENHANCE
109 // _(SOUND_MIXER_WRITE_LOUD, WRITE, sizeof(int)); // same as ...WRITE_ENHANCE
110 // _(SOUND_MIXER_WRITE_MUTE, WRITE, sizeof(int)); // same as ...WRITE_ENHANCE
111 _(BLKFLSBUF, NONE, 0);
112 _(BLKGETSIZE, WRITE, sizeof(uptr));
113 _(BLKRAGET, WRITE, sizeof(int));
114 _(BLKRASET, NONE, 0);
115 _(BLKROGET, WRITE, sizeof(int));
116 _(BLKROSET, READ, sizeof(int));
117 _(BLKRRPART, NONE, 0);
118 _(CDROMEJECT, NONE, 0);
119 _(CDROMEJECT_SW, NONE, 0);
120 _(CDROMMULTISESSION, WRITE, struct_cdrom_multisession_sz);
121 _(CDROMPAUSE, NONE, 0);
122 _(CDROMPLAYMSF, READ, struct_cdrom_msf_sz);
123 _(CDROMPLAYTRKIND, READ, struct_cdrom_ti_sz);
124 _(CDROMREADAUDIO, READ, struct_cdrom_read_audio_sz);
125 _(CDROMREADCOOKED, READ, struct_cdrom_msf_sz);
126 _(CDROMREADMODE1, READ, struct_cdrom_msf_sz);
127 _(CDROMREADMODE2, READ, struct_cdrom_msf_sz);
128 _(CDROMREADRAW, READ, struct_cdrom_msf_sz);
129 _(CDROMREADTOCENTRY, WRITE, struct_cdrom_tocentry_sz);
130 _(CDROMREADTOCHDR, WRITE, struct_cdrom_tochdr_sz);
131 _(CDROMRESET, NONE, 0);
132 _(CDROMRESUME, NONE, 0);
133 _(CDROMSEEK, READ, struct_cdrom_msf_sz);
134 _(CDROMSTART, NONE, 0);
135 _(CDROMSTOP, NONE, 0);
136 _(CDROMSUBCHNL, WRITE, struct_cdrom_subchnl_sz);
137 _(CDROMVOLCTRL, READ, struct_cdrom_volctrl_sz);
138 _(CDROMVOLREAD, WRITE, struct_cdrom_volctrl_sz);
139 _(CDROM_GET_UPC, WRITE, 8);
140 _(EVIOCGABS, WRITE, struct_input_absinfo_sz); // fixup
141 _(EVIOCGBIT, WRITE, struct_input_id_sz); // fixup
142 _(EVIOCGEFFECTS, WRITE, sizeof(int));
143 _(EVIOCGID, WRITE, struct_input_id_sz);
144 _(EVIOCGKEY, WRITE, 0);
145 _(EVIOCGKEYCODE, WRITE, sizeof(int) * 2);
146 _(EVIOCGLED, WRITE, 0);
147 _(EVIOCGNAME, WRITE, 0);
148 _(EVIOCGPHYS, WRITE, 0);
149 _(EVIOCGRAB, READ, sizeof(int));
150 _(EVIOCGREP, WRITE, sizeof(int) * 2);
151 _(EVIOCGSND, WRITE, 0);
152 _(EVIOCGSW, WRITE, 0);
153 _(EVIOCGUNIQ, WRITE, 0);
154 _(EVIOCGVERSION, WRITE, sizeof(int));
155 _(EVIOCRMFF, READ, sizeof(int));
156 _(EVIOCSABS, READ, struct_input_absinfo_sz); // fixup
157 _(EVIOCSFF, READ, struct_ff_effect_sz);
158 _(EVIOCSKEYCODE, READ, sizeof(int) * 2);
159 _(EVIOCSREP, READ, sizeof(int) * 2);
160 _(FDCLRPRM, NONE, 0);
161 _(FDDEFPRM, READ, struct_floppy_struct_sz);
163 _(FDFMTBEG, NONE, 0);
164 _(FDFMTEND, NONE, 0);
165 _(FDFMTTRK, READ, struct_format_descr_sz);
166 _(FDGETDRVPRM, WRITE, struct_floppy_drive_params_sz);
167 _(FDGETDRVSTAT, WRITE, struct_floppy_drive_struct_sz);
168 _(FDGETDRVTYP, WRITE, 16);
169 _(FDGETFDCSTAT, WRITE, struct_floppy_fdc_state_sz);
170 _(FDGETMAXERRS, WRITE, struct_floppy_max_errors_sz);
171 _(FDGETPRM, WRITE, struct_floppy_struct_sz);
172 _(FDMSGOFF, NONE, 0);
174 _(FDPOLLDRVSTAT, WRITE, struct_floppy_drive_struct_sz);
175 _(FDRAWCMD, WRITE, struct_floppy_raw_cmd_sz);
177 _(FDSETDRVPRM, READ, struct_floppy_drive_params_sz);
178 _(FDSETEMSGTRESH, NONE, 0);
179 _(FDSETMAXERRS, READ, struct_floppy_max_errors_sz);
180 _(FDSETPRM, READ, struct_floppy_struct_sz);
181 _(FDTWADDLE, NONE, 0);
182 _(FDWERRORCLR, NONE, 0);
183 _(FDWERRORGET, WRITE, struct_floppy_write_errors_sz);
184 _(HDIO_DRIVE_CMD, WRITE, sizeof(int));
185 _(HDIO_GETGEO, WRITE, struct_hd_geometry_sz);
186 _(HDIO_GET_32BIT, WRITE, sizeof(int));
187 _(HDIO_GET_DMA, WRITE, sizeof(int));
188 _(HDIO_GET_IDENTITY, WRITE, struct_hd_driveid_sz);
189 _(HDIO_GET_KEEPSETTINGS, WRITE, sizeof(int));
190 _(HDIO_GET_MULTCOUNT, WRITE, sizeof(int));
191 _(HDIO_GET_NOWERR, WRITE, sizeof(int));
192 _(HDIO_GET_UNMASKINTR, WRITE, sizeof(int));
193 _(HDIO_SET_32BIT, NONE, 0);
194 _(HDIO_SET_DMA, NONE, 0);
195 _(HDIO_SET_KEEPSETTINGS, NONE, 0);
196 _(HDIO_SET_MULTCOUNT, NONE, 0);
197 _(HDIO_SET_NOWERR, NONE, 0);
198 _(HDIO_SET_UNMASKINTR, NONE, 0);
199 _(MTIOCGET, WRITE, struct_mtget_sz);
200 _(MTIOCPOS, WRITE, struct_mtpos_sz);
201 _(MTIOCTOP, READ, struct_mtop_sz);
202 _(PPPIOCGASYNCMAP, WRITE, sizeof(int));
203 _(PPPIOCGDEBUG, WRITE, sizeof(int));
204 _(PPPIOCGFLAGS, WRITE, sizeof(int));
205 _(PPPIOCGUNIT, WRITE, sizeof(int));
206 _(PPPIOCGXASYNCMAP, WRITE, sizeof(int) * 8);
207 _(PPPIOCSASYNCMAP, READ, sizeof(int));
208 _(PPPIOCSDEBUG, READ, sizeof(int));
209 _(PPPIOCSFLAGS, READ, sizeof(int));
210 _(PPPIOCSMAXCID, READ, sizeof(int));
211 _(PPPIOCSMRU, READ, sizeof(int));
212 _(PPPIOCSXASYNCMAP, READ, sizeof(int) * 8);
213 _(SIOCADDRT, READ, struct_rtentry_sz);
214 _(SIOCDARP, READ, struct_arpreq_sz);
215 _(SIOCDELRT, READ, struct_rtentry_sz);
216 _(SIOCDRARP, READ, struct_arpreq_sz);
217 _(SIOCGARP, WRITE, struct_arpreq_sz);
218 _(SIOCGIFENCAP, WRITE, sizeof(int));
219 _(SIOCGIFHWADDR, WRITE, struct_ifreq_sz);
220 _(SIOCGIFMAP, WRITE, struct_ifreq_sz);
221 _(SIOCGIFMEM, WRITE, struct_ifreq_sz);
222 _(SIOCGIFNAME, NONE, 0);
223 _(SIOCGIFSLAVE, NONE, 0);
224 _(SIOCGRARP, WRITE, struct_arpreq_sz);
225 _(SIOCGSTAMP, WRITE, timeval_sz);
226 _(SIOCSARP, READ, struct_arpreq_sz);
227 _(SIOCSIFENCAP, READ, sizeof(int));
228 _(SIOCSIFHWADDR, READ, struct_ifreq_sz);
229 _(SIOCSIFLINK, NONE, 0);
230 _(SIOCSIFMAP, READ, struct_ifreq_sz);
231 _(SIOCSIFMEM, READ, struct_ifreq_sz);
232 _(SIOCSIFSLAVE, NONE, 0);
233 _(SIOCSRARP, READ, struct_arpreq_sz);
234 _(SNDCTL_COPR_HALT, WRITE, struct_copr_debug_buf_sz);
235 _(SNDCTL_COPR_LOAD, READ, struct_copr_buffer_sz);
236 _(SNDCTL_COPR_RCODE, WRITE, struct_copr_debug_buf_sz);
237 _(SNDCTL_COPR_RCVMSG, WRITE, struct_copr_msg_sz);
238 _(SNDCTL_COPR_RDATA, WRITE, struct_copr_debug_buf_sz);
239 _(SNDCTL_COPR_RESET, NONE, 0);
240 _(SNDCTL_COPR_RUN, WRITE, struct_copr_debug_buf_sz);
241 _(SNDCTL_COPR_SENDMSG, READ, struct_copr_msg_sz);
242 _(SNDCTL_COPR_WCODE, READ, struct_copr_debug_buf_sz);
243 _(SNDCTL_COPR_WDATA, READ, struct_copr_debug_buf_sz);
244 _(SNDCTL_DSP_GETBLKSIZE, WRITE, sizeof(int));
245 _(SNDCTL_DSP_GETFMTS, WRITE, sizeof(int));
246 _(SNDCTL_DSP_NONBLOCK, NONE, 0);
247 _(SNDCTL_DSP_POST, NONE, 0);
248 _(SNDCTL_DSP_RESET, NONE, 0);
249 _(SNDCTL_DSP_SETFMT, WRITE, sizeof(int));
250 _(SNDCTL_DSP_SETFRAGMENT, WRITE, sizeof(int));
251 _(SNDCTL_DSP_SPEED, WRITE, sizeof(int));
252 _(SNDCTL_DSP_STEREO, WRITE, sizeof(int));
253 _(SNDCTL_DSP_SUBDIVIDE, WRITE, sizeof(int));
254 _(SNDCTL_DSP_SYNC, NONE, 0);
255 _(SNDCTL_FM_4OP_ENABLE, READ, sizeof(int));
256 _(SNDCTL_FM_LOAD_INSTR, READ, struct_sbi_instrument_sz);
257 _(SNDCTL_MIDI_INFO, WRITE, struct_midi_info_sz);
258 _(SNDCTL_MIDI_PRETIME, WRITE, sizeof(int));
259 _(SNDCTL_SEQ_CTRLRATE, WRITE, sizeof(int));
260 _(SNDCTL_SEQ_GETINCOUNT, WRITE, sizeof(int));
261 _(SNDCTL_SEQ_GETOUTCOUNT, WRITE, sizeof(int));
262 _(SNDCTL_SEQ_NRMIDIS, WRITE, sizeof(int));
263 _(SNDCTL_SEQ_NRSYNTHS, WRITE, sizeof(int));
264 _(SNDCTL_SEQ_OUTOFBAND, READ, struct_seq_event_rec_sz);
265 _(SNDCTL_SEQ_PANIC, NONE, 0);
266 _(SNDCTL_SEQ_PERCMODE, NONE, 0);
267 _(SNDCTL_SEQ_RESET, NONE, 0);
268 _(SNDCTL_SEQ_RESETSAMPLES, READ, sizeof(int));
269 _(SNDCTL_SEQ_SYNC, NONE, 0);
270 _(SNDCTL_SEQ_TESTMIDI, READ, sizeof(int));
271 _(SNDCTL_SEQ_THRESHOLD, READ, sizeof(int));
272 _(SNDCTL_SYNTH_INFO, WRITE, struct_synth_info_sz);
273 _(SNDCTL_SYNTH_MEMAVL, WRITE, sizeof(int));
274 _(SNDCTL_TMR_METRONOME, READ, sizeof(int));
275 _(SNDCTL_TMR_SELECT, WRITE, sizeof(int));
276 _(SNDCTL_TMR_SOURCE, WRITE, sizeof(int));
277 _(SNDCTL_TMR_TEMPO, WRITE, sizeof(int));
278 _(SNDCTL_TMR_TIMEBASE, WRITE, sizeof(int));
279 _(SOUND_MIXER_READ_ALTPCM, WRITE, sizeof(int));
280 _(SOUND_MIXER_READ_BASS, WRITE, sizeof(int));
281 _(SOUND_MIXER_READ_CAPS, WRITE, sizeof(int));
282 _(SOUND_MIXER_READ_CD, WRITE, sizeof(int));
283 _(SOUND_MIXER_READ_DEVMASK, WRITE, sizeof(int));
284 _(SOUND_MIXER_READ_ENHANCE, WRITE, sizeof(int));
285 _(SOUND_MIXER_READ_IGAIN, WRITE, sizeof(int));
286 _(SOUND_MIXER_READ_IMIX, WRITE, sizeof(int));
287 _(SOUND_MIXER_READ_LINE, WRITE, sizeof(int));
288 _(SOUND_MIXER_READ_LINE1, WRITE, sizeof(int));
289 _(SOUND_MIXER_READ_LINE2, WRITE, sizeof(int));
290 _(SOUND_MIXER_READ_LINE3, WRITE, sizeof(int));
291 _(SOUND_MIXER_READ_MIC, WRITE, sizeof(int));
292 _(SOUND_MIXER_READ_OGAIN, WRITE, sizeof(int));
293 _(SOUND_MIXER_READ_PCM, WRITE, sizeof(int));
294 _(SOUND_MIXER_READ_RECLEV, WRITE, sizeof(int));
295 _(SOUND_MIXER_READ_RECMASK, WRITE, sizeof(int));
296 _(SOUND_MIXER_READ_RECSRC, WRITE, sizeof(int));
297 _(SOUND_MIXER_READ_SPEAKER, WRITE, sizeof(int));
298 _(SOUND_MIXER_READ_STEREODEVS, WRITE, sizeof(int));
299 _(SOUND_MIXER_READ_SYNTH, WRITE, sizeof(int));
300 _(SOUND_MIXER_READ_TREBLE, WRITE, sizeof(int));
301 _(SOUND_MIXER_READ_VOLUME, WRITE, sizeof(int));
302 _(SOUND_MIXER_WRITE_ALTPCM, WRITE, sizeof(int));
303 _(SOUND_MIXER_WRITE_BASS, WRITE, sizeof(int));
304 _(SOUND_MIXER_WRITE_CD, WRITE, sizeof(int));
305 _(SOUND_MIXER_WRITE_ENHANCE, WRITE, sizeof(int));
306 _(SOUND_MIXER_WRITE_IGAIN, WRITE, sizeof(int));
307 _(SOUND_MIXER_WRITE_IMIX, WRITE, sizeof(int));
308 _(SOUND_MIXER_WRITE_LINE, WRITE, sizeof(int));
309 _(SOUND_MIXER_WRITE_LINE1, WRITE, sizeof(int));
310 _(SOUND_MIXER_WRITE_LINE2, WRITE, sizeof(int));
311 _(SOUND_MIXER_WRITE_LINE3, WRITE, sizeof(int));
312 _(SOUND_MIXER_WRITE_MIC, WRITE, sizeof(int));
313 _(SOUND_MIXER_WRITE_OGAIN, WRITE, sizeof(int));
314 _(SOUND_MIXER_WRITE_PCM, WRITE, sizeof(int));
315 _(SOUND_MIXER_WRITE_RECLEV, WRITE, sizeof(int));
316 _(SOUND_MIXER_WRITE_RECSRC, WRITE, sizeof(int));
317 _(SOUND_MIXER_WRITE_SPEAKER, WRITE, sizeof(int));
318 _(SOUND_MIXER_WRITE_SYNTH, WRITE, sizeof(int));
319 _(SOUND_MIXER_WRITE_TREBLE, WRITE, sizeof(int));
320 _(SOUND_MIXER_WRITE_VOLUME, WRITE, sizeof(int));
321 _(SOUND_PCM_READ_BITS, WRITE, sizeof(int));
322 _(SOUND_PCM_READ_CHANNELS, WRITE, sizeof(int));
323 _(SOUND_PCM_READ_FILTER, WRITE, sizeof(int));
324 _(SOUND_PCM_READ_RATE, WRITE, sizeof(int));
325 _(SOUND_PCM_WRITE_CHANNELS, WRITE, sizeof(int));
326 _(SOUND_PCM_WRITE_FILTER, WRITE, sizeof(int));
328 _(TCGETA, WRITE, struct_termio_sz);
329 _(TCGETS, WRITE, struct_termios_sz);
332 _(TCSETA, READ, struct_termio_sz);
333 _(TCSETAF, READ, struct_termio_sz);
334 _(TCSETAW, READ, struct_termio_sz);
335 _(TCSETS, READ, struct_termios_sz);
336 _(TCSETSF, READ, struct_termios_sz);
337 _(TCSETSW, READ, struct_termios_sz);
339 _(TIOCGLCKTRMIOS, WRITE, struct_termios_sz);
340 _(TIOCGSOFTCAR, WRITE, sizeof(int));
341 _(TIOCINQ, WRITE, sizeof(int));
342 _(TIOCLINUX, READ, sizeof(char));
343 _(TIOCSERCONFIG, NONE, 0);
344 _(TIOCSERGETLSR, WRITE, sizeof(int));
345 _(TIOCSERGWILD, WRITE, sizeof(int));
346 _(TIOCSERSWILD, READ, sizeof(int));
347 _(TIOCSLCKTRMIOS, READ, struct_termios_sz);
348 _(TIOCSSOFTCAR, READ, sizeof(int));
349 _(VT_ACTIVATE, NONE, 0);
350 _(VT_DISALLOCATE, NONE, 0);
351 _(VT_GETMODE, WRITE, struct_vt_mode_sz);
352 _(VT_GETSTATE, WRITE, struct_vt_stat_sz);
353 _(VT_OPENQRY, WRITE, sizeof(int));
354 _(VT_RELDISP, NONE, 0);
355 _(VT_RESIZE, READ, struct_vt_sizes_sz);
356 _(VT_RESIZEX, READ, struct_vt_consize_sz);
357 _(VT_SENDSIG, NONE, 0);
358 _(VT_SETMODE, READ, struct_vt_mode_sz);
359 _(VT_WAITACTIVE, NONE, 0);
362 #if SANITIZER_LINUX && !SANITIZER_ANDROID
363 // _(SIOCDEVPLIP, WRITE, struct_ifreq_sz); // the same as EQL_ENSLAVE
364 _(CYGETDEFTHRESH, WRITE, sizeof(int));
365 _(CYGETDEFTIMEOUT, WRITE, sizeof(int));
366 _(CYGETMON, WRITE, struct_cyclades_monitor_sz);
367 _(CYGETTHRESH, WRITE, sizeof(int));
368 _(CYGETTIMEOUT, WRITE, sizeof(int));
369 _(CYSETDEFTHRESH, NONE, 0);
370 _(CYSETDEFTIMEOUT, NONE, 0);
371 _(CYSETTHRESH, NONE, 0);
372 _(CYSETTIMEOUT, NONE, 0);
373 _(EQL_EMANCIPATE, WRITE, struct_ifreq_sz);
374 _(EQL_ENSLAVE, WRITE, struct_ifreq_sz);
375 _(EQL_GETMASTRCFG, WRITE, struct_ifreq_sz);
376 _(EQL_GETSLAVECFG, WRITE, struct_ifreq_sz);
377 _(EQL_SETMASTRCFG, WRITE, struct_ifreq_sz);
378 _(EQL_SETSLAVECFG, WRITE, struct_ifreq_sz);
379 _(EVIOCGKEYCODE_V2, WRITE, struct_input_keymap_entry_sz);
380 _(EVIOCGPROP, WRITE, 0);
381 _(EVIOCSKEYCODE_V2, READ, struct_input_keymap_entry_sz);
382 _(FS_IOC_GETFLAGS, WRITE, sizeof(int));
383 _(FS_IOC_GETVERSION, WRITE, sizeof(int));
384 _(FS_IOC_SETFLAGS, READ, sizeof(int));
385 _(FS_IOC_SETVERSION, READ, sizeof(int));
386 _(GIO_CMAP, WRITE, 48);
387 _(GIO_FONT, WRITE, 8192);
388 _(GIO_SCRNMAP, WRITE, e_tabsz);
389 _(GIO_UNIMAP, WRITE, struct_unimapdesc_sz);
390 _(GIO_UNISCRNMAP, WRITE, sizeof(short) * e_tabsz);
393 _(KDDISABIO, NONE, 0);
394 _(KDENABIO, NONE, 0);
395 _(KDGETKEYCODE, WRITE, struct_kbkeycode_sz);
396 _(KDGETLED, WRITE, 1);
397 _(KDGETMODE, WRITE, sizeof(int));
398 _(KDGKBDIACR, WRITE, struct_kbdiacrs_sz);
399 _(KDGKBENT, WRITE, struct_kbentry_sz);
400 _(KDGKBLED, WRITE, sizeof(int));
401 _(KDGKBMETA, WRITE, sizeof(int));
402 _(KDGKBMODE, WRITE, sizeof(int));
403 _(KDGKBSENT, WRITE, struct_kbsentry_sz);
404 _(KDGKBTYPE, WRITE, 1);
405 _(KDMAPDISP, NONE, 0);
406 _(KDMKTONE, NONE, 0);
407 _(KDSETKEYCODE, READ, struct_kbkeycode_sz);
408 _(KDSETLED, NONE, 0);
409 _(KDSETMODE, NONE, 0);
410 _(KDSIGACCEPT, NONE, 0);
411 _(KDSKBDIACR, READ, struct_kbdiacrs_sz);
412 _(KDSKBENT, READ, struct_kbentry_sz);
413 _(KDSKBLED, NONE, 0);
414 _(KDSKBMETA, NONE, 0);
415 _(KDSKBMODE, NONE, 0);
416 _(KDSKBSENT, READ, struct_kbsentry_sz);
417 _(KDUNMAPDISP, NONE, 0);
418 _(KIOCSOUND, NONE, 0);
420 _(LPABORTOPEN, NONE, 0);
421 _(LPCAREFUL, NONE, 0);
423 _(LPGETIRQ, WRITE, sizeof(int));
424 _(LPGETSTATUS, WRITE, sizeof(int));
426 _(LPSETIRQ, NONE, 0);
429 _(MTIOCGETCONFIG, WRITE, struct_mtconfiginfo_sz);
430 _(MTIOCSETCONFIG, READ, struct_mtconfiginfo_sz);
431 _(PIO_CMAP, NONE, 0);
432 _(PIO_FONT, READ, 8192);
433 _(PIO_SCRNMAP, READ, e_tabsz);
434 _(PIO_UNIMAP, READ, struct_unimapdesc_sz);
435 _(PIO_UNIMAPCLR, READ, struct_unimapinit_sz);
436 _(PIO_UNISCRNMAP, READ, sizeof(short) * e_tabsz);
437 _(SCSI_IOCTL_PROBE_HOST, READ, sizeof(int));
438 _(SCSI_IOCTL_TAGGED_DISABLE, NONE, 0);
439 _(SCSI_IOCTL_TAGGED_ENABLE, NONE, 0);
440 _(SNDCTL_DSP_GETISPACE, WRITE, struct_audio_buf_info_sz);
441 _(SNDCTL_DSP_GETOSPACE, WRITE, struct_audio_buf_info_sz);
442 _(TIOCGSERIAL, WRITE, struct_serial_struct_sz);
443 _(TIOCSERGETMULTI, WRITE, struct_serial_multiport_struct_sz);
444 _(TIOCSERSETMULTI, READ, struct_serial_multiport_struct_sz);
445 _(TIOCSSERIAL, READ, struct_serial_struct_sz);
447 // The following ioctl requests are shared between AX25, IPX, netrom and
449 // _(SIOCAIPXITFCRT, READ, sizeof(char));
450 // _(SIOCAX25GETUID, READ, struct_sockaddr_ax25_sz);
451 // _(SIOCNRGETPARMS, WRITE, struct_nr_parms_struct_sz);
452 // _(SIOCAIPXPRISLT, READ, sizeof(char));
453 // _(SIOCNRSETPARMS, READ, struct_nr_parms_struct_sz);
454 // _(SIOCAX25ADDUID, READ, struct_sockaddr_ax25_sz);
455 // _(SIOCNRDECOBS, NONE, 0);
456 // _(SIOCAX25DELUID, READ, struct_sockaddr_ax25_sz);
457 // _(SIOCIPXCFGDATA, WRITE, struct_ipx_config_data_sz);
458 // _(SIOCAX25NOUID, READ, sizeof(int));
459 // _(SIOCNRRTCTL, READ, sizeof(int));
460 // _(SIOCAX25DIGCTL, READ, sizeof(int));
461 // _(SIOCAX25GETPARMS, WRITE, struct_ax25_parms_struct_sz);
462 // _(SIOCAX25SETPARMS, READ, struct_ax25_parms_struct_sz);
467 static bool ioctl_initialized = false;
469 struct ioctl_desc_compare {
470 bool operator()(const ioctl_desc& left, const ioctl_desc& right) const {
471 return left.req < right.req;
475 static void ioctl_init() {
477 InternalSort(&ioctl_table, ioctl_table_size, ioctl_desc_compare());
480 for (unsigned i = 0; i < ioctl_table_size - 1; ++i) {
481 if (ioctl_table[i].req >= ioctl_table[i + 1].req) {
482 Printf("Duplicate or unsorted ioctl request id %x >= %x (%s vs %s)\n",
483 ioctl_table[i].req, ioctl_table[i + 1].req, ioctl_table[i].name,
484 ioctl_table[i + 1].name);
491 ioctl_initialized = true;
494 // Handle the most evil ioctls that encode argument value as part of request id.
495 static unsigned ioctl_request_fixup(unsigned req) {
497 // Strip size and event number.
498 const unsigned kEviocgbitMask =
499 (IOC_SIZEMASK << IOC_SIZESHIFT) | EVIOC_EV_MAX;
500 if ((req & ~kEviocgbitMask) == IOCTL_EVIOCGBIT)
501 return IOCTL_EVIOCGBIT;
502 // Strip absolute axis number.
503 if ((req & ~EVIOC_ABS_MAX) == IOCTL_EVIOCGABS)
504 return IOCTL_EVIOCGABS;
505 if ((req & ~EVIOC_ABS_MAX) == IOCTL_EVIOCSABS)
506 return IOCTL_EVIOCSABS;
511 static const ioctl_desc *ioctl_table_lookup(unsigned req) {
513 int right = ioctl_table_size;
514 while (left < right) {
515 int mid = (left + right) / 2;
516 if (ioctl_table[mid].req < req)
521 if (left == right && ioctl_table[left].req == req)
522 return ioctl_table + left;
527 static bool ioctl_decode(unsigned req, ioctl_desc *desc) {
530 desc->name = "<DECODED_IOCTL>";
531 desc->size = IOC_SIZE(req);
533 if (desc->size > 0xFFFF) return false;
534 unsigned dir = IOC_DIR(req);
537 desc->type = ioctl_desc::NONE;
539 case IOC_READ | IOC_WRITE:
540 desc->type = ioctl_desc::READWRITE;
543 desc->type = ioctl_desc::WRITE;
546 desc->type = ioctl_desc::READ;
551 // Size can be 0 iff type is NONE.
552 if ((desc->type == IOC_NONE) != (desc->size == 0)) return false;
554 if (IOC_TYPE(req) == 0) return false;
558 static const ioctl_desc *ioctl_lookup(unsigned req) {
559 req = ioctl_request_fixup(req);
560 const ioctl_desc *desc = ioctl_table_lookup(req);
561 if (desc) return desc;
563 // Try stripping access size from the request id.
564 desc = ioctl_table_lookup(req & ~(IOC_SIZEMASK << IOC_SIZESHIFT));
565 // Sanity check: requests that encode access size are either read or write and
566 // have size of 0 in the table.
567 if (desc && desc->size == 0 &&
568 (desc->type == ioctl_desc::READWRITE || desc->type == ioctl_desc::WRITE ||
569 desc->type == ioctl_desc::READ))
574 static void ioctl_common_pre(void *ctx, const ioctl_desc *desc, int d,
575 unsigned request, void *arg) {
576 if (desc->type == ioctl_desc::READ || desc->type == ioctl_desc::READWRITE) {
577 unsigned size = desc->size ? desc->size : IOC_SIZE(request);
578 COMMON_INTERCEPTOR_READ_RANGE(ctx, arg, size);
580 if (desc->type != ioctl_desc::CUSTOM)
582 if (request == IOCTL_SIOCGIFCONF) {
583 struct __sanitizer_ifconf *ifc = (__sanitizer_ifconf *)arg;
584 COMMON_INTERCEPTOR_READ_RANGE(ctx, (char*)&ifc->ifc_len,
585 sizeof(ifc->ifc_len));
589 static void ioctl_common_post(void *ctx, const ioctl_desc *desc, int res, int d,
590 unsigned request, void *arg) {
591 if (desc->type == ioctl_desc::WRITE || desc->type == ioctl_desc::READWRITE) {
592 // FIXME: add verbose output
593 unsigned size = desc->size ? desc->size : IOC_SIZE(request);
594 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, arg, size);
596 if (desc->type != ioctl_desc::CUSTOM)
598 if (request == IOCTL_SIOCGIFCONF) {
599 struct __sanitizer_ifconf *ifc = (__sanitizer_ifconf *)arg;
600 COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifc->ifc_ifcu.ifcu_req, ifc->ifc_len);