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