Skip to content

Commit b7abf78

Browse files
andrewshaduraJiri Kosina
authored andcommitted
HID: u2fzero: clarify error check and length calculations
The previous commit fixed handling of incomplete packets but broke error handling: offsetof returns an unsigned value (size_t), but when compared against the signed return value, the return value is interpreted as if it were unsigned, so negative return values are never less than the offset. To make the code easier to read, calculate the minimal packet length once and separately, and assign it to a signed int variable to eliminate unsigned math and the need for type casts. It then becomes immediately obvious how the actual data length is calculated and why the return value cannot be less than the minimal length. Fixes: 22d6576 ("HID: u2fzero: ignore incomplete packets without data") Fixes: 42337b9 ("HID: add driver for U2F Zero built-in LED and RNG") Signed-off-by: Andrej Shadura <andrew.shadura@collabora.co.uk> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent 08b9a61 commit b7abf78

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

drivers/hid/hid-u2fzero.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ static int u2fzero_rng_read(struct hwrng *rng, void *data,
191191
struct u2f_hid_msg resp;
192192
int ret;
193193
size_t actual_length;
194+
/* valid packets must have a correct header */
195+
int min_length = offsetof(struct u2f_hid_msg, init.data);
194196

195197
if (!dev->present) {
196198
hid_dbg(dev->hdev, "device not present");
@@ -200,12 +202,12 @@ static int u2fzero_rng_read(struct hwrng *rng, void *data,
200202
ret = u2fzero_recv(dev, &req, &resp);
201203

202204
/* ignore errors or packets without data */
203-
if (ret < offsetof(struct u2f_hid_msg, init.data))
205+
if (ret < min_length)
204206
return 0;
205207

206208
/* only take the minimum amount of data it is safe to take */
207-
actual_length = min3((size_t)ret - offsetof(struct u2f_hid_msg,
208-
init.data), U2F_HID_MSG_LEN(resp), max);
209+
actual_length = min3((size_t)ret - min_length,
210+
U2F_HID_MSG_LEN(resp), max);
209211

210212
memcpy(data, resp.init.data, actual_length);
211213

0 commit comments

Comments
 (0)