DF-2071 / fix.diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | diff --git a/sys/dev/misc/kbd/kbdsw.c b/sys/dev/misc/kbd/kbdsw.c --- a/sys/dev/misc/kbd/kbdsw.c +++ b/sys/dev/misc/kbd/kbdsw.c @@ -52,6 +52,24 @@ #include "kbdreg.h" +/* + * Defense-in-depth (DF-2071): only kbd_intr() previously validated + * kb_index before indexing the global kbdsw[] dispatch table. The other + * 16 dispatch shims blindly dereferenced kbdsw[kbd->kb_index], which is + * unsafe if a caller hands us a stale keyboard_t whose switch has been + * unregistered (kbdsw[i] = NULL in kbd.c:282) or whose kb_index is + * corrupt. kb_index is assigned at kbd_register() time and validated + * against KBD_MAXKEYBOARDS, but nothing prevents a buggy or racing + * caller from passing a stale pointer after kbd_unregister() clears the + * slot. The bound + non-NULL check below mirrors the guard already + * present in kbd_intr() (kbdsw.c:103). + */ +static inline bool +kbdsw_index_valid(int i) +{ + return (i >= 0 && i < KBD_MAXKEYBOARDS && kbdsw[i] != NULL); +} + int sw_probe(keyboard_switch_t *sw, int unit, void *arg, int flags) { @@ -76,6 +94,8 @@ { int error; + if (!kbdsw_index_valid(kbd->kb_index)) + return (ENODEV); KBD_ALWAYS_LOCK(kbd); error = (*kbdsw[kbd->kb_index]->term)(kbd); if (error) @@ -114,6 +134,8 @@ int error; KBD_LOCK_DECLARE; + if (!kbdsw_index_valid(kbd->kb_index)) + return (ENODEV); KBD_LOCK(kbd); error = (*kbdsw[kbd->kb_index]->test_if)(kbd); KBD_UNLOCK(kbd); @@ -127,6 +149,8 @@ int error; KBD_LOCK_DECLARE; + if (!kbdsw_index_valid(kbd->kb_index)) + return (ENODEV); KBD_LOCK(kbd); error = (*kbdsw[kbd->kb_index]->enable)(kbd); KBD_UNLOCK(kbd); @@ -140,6 +164,8 @@ int error; KBD_LOCK_DECLARE; + if (!kbdsw_index_valid(kbd->kb_index)) + return (ENODEV); KBD_LOCK(kbd); error = (*kbdsw[kbd->kb_index]->disable)(kbd); KBD_UNLOCK(kbd); @@ -153,6 +179,8 @@ int error; KBD_LOCK_DECLARE; + if (!kbdsw_index_valid(kbd->kb_index)) + return (ENODEV); KBD_LOCK(kbd); error = (*kbdsw[kbd->kb_index]->read)(kbd, wait); KBD_UNLOCK(kbd); @@ -166,6 +194,8 @@ int error; KBD_LOCK_DECLARE; + if (!kbdsw_index_valid(kbd->kb_index)) + return (ENODEV); KBD_LOCK(kbd); error = (*kbdsw[kbd->kb_index]->check)(kbd); KBD_UNLOCK(kbd); @@ -179,6 +209,8 @@ int error; KBD_LOCK_DECLARE; + if (!kbdsw_index_valid(kbd->kb_index)) + return (NOKEY); KBD_LOCK(kbd); error = (*kbdsw[kbd->kb_index]->read_char)(kbd, wait); KBD_UNLOCK(kbd); @@ -192,6 +224,8 @@ int error; KBD_LOCK_DECLARE; + if (!kbdsw_index_valid(kbd->kb_index)) + return (ENODEV); KBD_LOCK(kbd); error = (*kbdsw[kbd->kb_index]->check_char)(kbd); KBD_UNLOCK(kbd); @@ -205,13 +239,13 @@ int error; KBD_LOCK_DECLARE; - if (kbd) { - KBD_LOCK(kbd); - error = (*kbdsw[kbd->kb_index]->ioctl)(kbd, cmd, data); - KBD_UNLOCK(kbd); - } else { - error = ENODEV; - } + if (kbd == NULL) + return (ENODEV); + if (!kbdsw_index_valid(kbd->kb_index)) + return (ENODEV); + KBD_LOCK(kbd); + error = (*kbdsw[kbd->kb_index]->ioctl)(kbd, cmd, data); + KBD_UNLOCK(kbd); return (error); } @@ -221,6 +255,8 @@ int error; KBD_LOCK_DECLARE; + if (!kbdsw_index_valid(kbd->kb_index)) + return (ENODEV); KBD_LOCK(kbd); error = (*kbdsw[kbd->kb_index]->lock)(kbd, xlock); KBD_UNLOCK(kbd); @@ -233,6 +269,8 @@ { KBD_LOCK_DECLARE; + if (!kbdsw_index_valid(kbd->kb_index)) + return; KBD_LOCK(kbd); (*kbdsw[kbd->kb_index]->clear_state)(kbd); KBD_UNLOCK(kbd); @@ -244,6 +282,8 @@ int error; KBD_LOCK_DECLARE; + if (!kbdsw_index_valid(kbd->kb_index)) + return (ENODEV); KBD_LOCK(kbd); error = (*kbdsw[kbd->kb_index]->get_state)(kbd, buf, len); KBD_UNLOCK(kbd); @@ -257,6 +297,8 @@ int error; KBD_LOCK_DECLARE; + if (!kbdsw_index_valid(kbd->kb_index)) + return (ENODEV); KBD_LOCK(kbd); error = (*kbdsw[kbd->kb_index]->set_state)(kbd, buf, len); KBD_UNLOCK(kbd); @@ -270,6 +312,8 @@ KBD_LOCK_DECLARE; u_char *retstr; + if (!kbdsw_index_valid(kbd->kb_index)) + return (NULL); KBD_LOCK(kbd); retstr = (*kbdsw[kbd->kb_index]->get_fkeystr)(kbd, fkey, len); KBD_UNLOCK(kbd); @@ -285,6 +329,8 @@ { int error; + if (!kbdsw_index_valid(kbd->kb_index)) + return (ENODEV); if (!on) KBD_UNPOLL(kbd); error = (*kbdsw[kbd->kb_index]->poll)(kbd, on); @@ -299,6 +345,8 @@ { KBD_LOCK_DECLARE; + if (!kbdsw_index_valid(kbd->kb_index)) + return; KBD_LOCK(kbd); (*kbdsw[kbd->kb_index]->diag)(kbd, level); KBD_UNLOCK(kbd); |