Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Input: uinput - allow injecting event times

Currently, uinput doesn't use the input_set_timestamp API, so any
event injected using uinput is not accurately timestamped in terms of
measuring when the actual event happened. Hence, call the
input_set_timestamp API from uinput in order to provide a more
accurate sense of time for the event. Propagate only the timestamps
which are a) positive, b) within a pre-defined offset (10 secs) from
the current time, and c) not in the future.

Signed-off-by: Biswarup Pal <biswarupp@google.com>
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Siarhei Vishniakou <svv@google.com>
Link: https://lore.kernel.org/r/20230427000152.1407471-1-biswarupp@google.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

authored by

Biswarup Pal and committed by
Dmitry Torokhov
3a2df602 97804321

+34
+34
drivers/input/misc/uinput.c
··· 33 33 #define UINPUT_NAME "uinput" 34 34 #define UINPUT_BUFFER_SIZE 16 35 35 #define UINPUT_NUM_REQUESTS 16 36 + #define UINPUT_TIMESTAMP_ALLOWED_OFFSET_SECS 10 36 37 37 38 enum uinput_state { UIST_NEW_DEVICE, UIST_SETUP_COMPLETE, UIST_CREATED }; 38 39 ··· 570 569 return retval; 571 570 } 572 571 572 + /* 573 + * Returns true if the given timestamp is valid (i.e., if all the following 574 + * conditions are satisfied), false otherwise. 575 + * 1) given timestamp is positive 576 + * 2) it's within the allowed offset before the current time 577 + * 3) it's not in the future 578 + */ 579 + static bool is_valid_timestamp(const ktime_t timestamp) 580 + { 581 + ktime_t zero_time; 582 + ktime_t current_time; 583 + ktime_t min_time; 584 + ktime_t offset; 585 + 586 + zero_time = ktime_set(0, 0); 587 + if (ktime_compare(zero_time, timestamp) >= 0) 588 + return false; 589 + 590 + current_time = ktime_get(); 591 + offset = ktime_set(UINPUT_TIMESTAMP_ALLOWED_OFFSET_SECS, 0); 592 + min_time = ktime_sub(current_time, offset); 593 + 594 + if (ktime_after(min_time, timestamp) || ktime_after(timestamp, current_time)) 595 + return false; 596 + 597 + return true; 598 + } 599 + 573 600 static ssize_t uinput_inject_events(struct uinput_device *udev, 574 601 const char __user *buffer, size_t count) 575 602 { 576 603 struct input_event ev; 577 604 size_t bytes = 0; 605 + ktime_t timestamp; 578 606 579 607 if (count != 0 && count < input_event_size()) 580 608 return -EINVAL; ··· 617 587 */ 618 588 if (input_event_from_user(buffer + bytes, &ev)) 619 589 return -EFAULT; 590 + 591 + timestamp = ktime_set(ev.input_event_sec, ev.input_event_usec * NSEC_PER_USEC); 592 + if (is_valid_timestamp(timestamp)) 593 + input_set_timestamp(udev->dev, timestamp); 620 594 621 595 input_event(udev->dev, ev.type, ev.code, ev.value); 622 596 bytes += input_event_size();