~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/Documentation/usb/usbdevfs-drop-permissions.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php