1*4882a593Smuzhiyun #include <sys/ioctl.h>
2*4882a593Smuzhiyun #include <sys/types.h>
3*4882a593Smuzhiyun #include <sys/stat.h>
4*4882a593Smuzhiyun #include <fcntl.h>
5*4882a593Smuzhiyun #include <stdio.h>
6*4882a593Smuzhiyun #include <errno.h>
7*4882a593Smuzhiyun #include <string.h>
8*4882a593Smuzhiyun #include <inttypes.h>
9*4882a593Smuzhiyun #include <unistd.h>
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/usbdevice_fs.h>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun /* For building without an updated set of headers */
14*4882a593Smuzhiyun #ifndef USBDEVFS_DROP_PRIVILEGES
15*4882a593Smuzhiyun #define USBDEVFS_DROP_PRIVILEGES _IOW('U', 30, __u32)
16*4882a593Smuzhiyun #define USBDEVFS_CAP_DROP_PRIVILEGES 0x40
17*4882a593Smuzhiyun #endif
18*4882a593Smuzhiyun
drop_privileges(int fd,uint32_t mask)19*4882a593Smuzhiyun void drop_privileges(int fd, uint32_t mask)
20*4882a593Smuzhiyun {
21*4882a593Smuzhiyun int res;
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun res = ioctl(fd, USBDEVFS_DROP_PRIVILEGES, &mask);
24*4882a593Smuzhiyun if (res)
25*4882a593Smuzhiyun printf("ERROR: USBDEVFS_DROP_PRIVILEGES returned %d\n", res);
26*4882a593Smuzhiyun else
27*4882a593Smuzhiyun printf("OK: privileges dropped!\n");
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun
reset_device(int fd)30*4882a593Smuzhiyun void reset_device(int fd)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun int res;
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun res = ioctl(fd, USBDEVFS_RESET);
35*4882a593Smuzhiyun if (!res)
36*4882a593Smuzhiyun printf("OK: USBDEVFS_RESET succeeded\n");
37*4882a593Smuzhiyun else
38*4882a593Smuzhiyun printf("ERROR: reset failed! (%d - %s)\n",
39*4882a593Smuzhiyun -res, strerror(-res));
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun
claim_some_intf(int fd)42*4882a593Smuzhiyun void claim_some_intf(int fd)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun int i, res;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun for (i = 0; i < 4; i++) {
47*4882a593Smuzhiyun res = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &i);
48*4882a593Smuzhiyun if (!res)
49*4882a593Smuzhiyun printf("OK: claimed if %d\n", i);
50*4882a593Smuzhiyun else
51*4882a593Smuzhiyun printf("ERROR claiming if %d (%d - %s)\n",
52*4882a593Smuzhiyun i, -res, strerror(-res));
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun
main(int argc,char * argv[])56*4882a593Smuzhiyun int main(int argc, char *argv[])
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun uint32_t mask, caps;
59*4882a593Smuzhiyun int c, fd;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun fd = open(argv[1], O_RDWR);
62*4882a593Smuzhiyun if (fd < 0) {
63*4882a593Smuzhiyun printf("Failed to open file\n");
64*4882a593Smuzhiyun goto err_fd;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /*
68*4882a593Smuzhiyun * check if dropping privileges is supported,
69*4882a593Smuzhiyun * bail on systems where the capability is not present
70*4882a593Smuzhiyun */
71*4882a593Smuzhiyun ioctl(fd, USBDEVFS_GET_CAPABILITIES, &caps);
72*4882a593Smuzhiyun if (!(caps & USBDEVFS_CAP_DROP_PRIVILEGES)) {
73*4882a593Smuzhiyun printf("DROP_PRIVILEGES not supported\n");
74*4882a593Smuzhiyun goto err;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /*
78*4882a593Smuzhiyun * Drop privileges but keep the ability to claim all
79*4882a593Smuzhiyun * free interfaces (i.e., those not used by kernel drivers)
80*4882a593Smuzhiyun */
81*4882a593Smuzhiyun drop_privileges(fd, -1U);
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun printf("Available options:\n"
84*4882a593Smuzhiyun "[0] Exit now\n"
85*4882a593Smuzhiyun "[1] Reset device. Should fail if device is in use\n"
86*4882a593Smuzhiyun "[2] Claim 4 interfaces. Should succeed where not in use\n"
87*4882a593Smuzhiyun "[3] Narrow interface permission mask\n"
88*4882a593Smuzhiyun "Which option shall I run?: ");
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun while (scanf("%d", &c) == 1) {
91*4882a593Smuzhiyun switch (c) {
92*4882a593Smuzhiyun case 0:
93*4882a593Smuzhiyun goto exit;
94*4882a593Smuzhiyun case 1:
95*4882a593Smuzhiyun reset_device(fd);
96*4882a593Smuzhiyun break;
97*4882a593Smuzhiyun case 2:
98*4882a593Smuzhiyun claim_some_intf(fd);
99*4882a593Smuzhiyun break;
100*4882a593Smuzhiyun case 3:
101*4882a593Smuzhiyun printf("Insert new mask: ");
102*4882a593Smuzhiyun scanf("%x", &mask);
103*4882a593Smuzhiyun drop_privileges(fd, mask);
104*4882a593Smuzhiyun break;
105*4882a593Smuzhiyun default:
106*4882a593Smuzhiyun printf("I don't recognize that\n");
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun printf("Which test shall I run next?: ");
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun exit:
113*4882a593Smuzhiyun close(fd);
114*4882a593Smuzhiyun return 0;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun err:
117*4882a593Smuzhiyun close(fd);
118*4882a593Smuzhiyun err_fd:
119*4882a593Smuzhiyun return 1;
120*4882a593Smuzhiyun }
121