29
29
#include <sys/socket.h>
30
30
#include <sys/un.h>
31
31
#include <linux/netlink.h>
32
+
33
+ #ifdef HAVE_SELINUX
34
+ #include <selinux/selinux.h>
35
+ #include <selinux/label.h>
36
+ #endif
37
+
32
38
#include <private/android_filesystem_config.h>
33
39
#include <sys/time.h>
34
40
#include <asm/page.h>
45
51
#define FIRMWARE_DIR1 "/etc/firmware"
46
52
#define FIRMWARE_DIR2 "/vendor/firmware"
47
53
54
+ #ifdef HAVE_SELINUX
55
+ static struct selabel_handle * sehandle ;
56
+ #endif
57
+
48
58
static int device_fd = -1 ;
49
59
50
60
struct uevent {
@@ -180,8 +190,17 @@ static void make_device(const char *path,
180
190
unsigned gid ;
181
191
mode_t mode ;
182
192
dev_t dev ;
193
+ #ifdef HAVE_SELINUX
194
+ char * secontext = NULL ;
195
+ #endif
183
196
184
197
mode = get_device_perm (path , & uid , & gid ) | (block ? S_IFBLK : S_IFCHR );
198
+ #ifdef HAVE_SELINUX
199
+ if (sehandle ) {
200
+ selabel_lookup (sehandle , & secontext , path , mode );
201
+ setfscreatecon (secontext );
202
+ }
203
+ #endif
185
204
dev = makedev (major , minor );
186
205
/* Temporarily change egid to avoid race condition setting the gid of the
187
206
* device node. Unforunately changing the euid would prevent creation of
@@ -192,8 +211,40 @@ static void make_device(const char *path,
192
211
mknod (path , mode , dev );
193
212
chown (path , uid , -1 );
194
213
setegid (AID_ROOT );
214
+ #ifdef HAVE_SELINUX
215
+ if (secontext ) {
216
+ freecon (secontext );
217
+ setfscreatecon (NULL );
218
+ }
219
+ #endif
220
+ }
221
+
222
+
223
+ static int make_dir (const char * path , mode_t mode )
224
+ {
225
+ int rc ;
226
+
227
+ #ifdef HAVE_SELINUX
228
+ char * secontext = NULL ;
229
+
230
+ if (sehandle ) {
231
+ selabel_lookup (sehandle , & secontext , path , mode );
232
+ setfscreatecon (secontext );
233
+ }
234
+ #endif
235
+
236
+ rc = mkdir (path , mode );
237
+
238
+ #ifdef HAVE_SELINUX
239
+ if (secontext ) {
240
+ freecon (secontext );
241
+ setfscreatecon (NULL );
242
+ }
243
+ #endif
244
+ return rc ;
195
245
}
196
246
247
+
197
248
static void add_platform_device (const char * name )
198
249
{
199
250
int name_len = strlen (name );
@@ -506,7 +557,7 @@ static void handle_block_device_event(struct uevent *uevent)
506
557
return ;
507
558
508
559
snprintf (devpath , sizeof (devpath ), "%s%s" , base , name );
509
- mkdir (base , 0755 );
560
+ make_dir (base , 0755 );
510
561
511
562
if (!strncmp (uevent -> path , "/devices/platform/" , 18 ))
512
563
links = parse_platform_block_device (uevent );
@@ -535,40 +586,40 @@ static void handle_generic_device_event(struct uevent *uevent)
535
586
int bus_id = uevent -> minor / 128 + 1 ;
536
587
int device_id = uevent -> minor % 128 + 1 ;
537
588
/* build directories */
538
- mkdir ("/dev/bus" , 0755 );
539
- mkdir ("/dev/bus/usb" , 0755 );
589
+ make_dir ("/dev/bus" , 0755 );
590
+ make_dir ("/dev/bus/usb" , 0755 );
540
591
snprintf (devpath , sizeof (devpath ), "/dev/bus/usb/%03d" , bus_id );
541
- mkdir (devpath , 0755 );
592
+ make_dir (devpath , 0755 );
542
593
snprintf (devpath , sizeof (devpath ), "/dev/bus/usb/%03d/%03d" , bus_id , device_id );
543
594
} else {
544
595
/* ignore other USB events */
545
596
return ;
546
597
}
547
598
} else if (!strncmp (uevent -> subsystem , "graphics" , 8 )) {
548
599
base = "/dev/graphics/" ;
549
- mkdir (base , 0755 );
600
+ make_dir (base , 0755 );
550
601
} else if (!strncmp (uevent -> subsystem , "oncrpc" , 6 )) {
551
602
base = "/dev/oncrpc/" ;
552
- mkdir (base , 0755 );
603
+ make_dir (base , 0755 );
553
604
} else if (!strncmp (uevent -> subsystem , "adsp" , 4 )) {
554
605
base = "/dev/adsp/" ;
555
- mkdir (base , 0755 );
606
+ make_dir (base , 0755 );
556
607
} else if (!strncmp (uevent -> subsystem , "msm_camera" , 10 )) {
557
608
base = "/dev/msm_camera/" ;
558
- mkdir (base , 0755 );
609
+ make_dir (base , 0755 );
559
610
} else if (!strncmp (uevent -> subsystem , "input" , 5 )) {
560
611
base = "/dev/input/" ;
561
- mkdir (base , 0755 );
612
+ make_dir (base , 0755 );
562
613
} else if (!strncmp (uevent -> subsystem , "mtd" , 3 )) {
563
614
base = "/dev/mtd/" ;
564
- mkdir (base , 0755 );
615
+ make_dir (base , 0755 );
565
616
} else if (!strncmp (uevent -> subsystem , "sound" , 5 )) {
566
617
base = "/dev/snd/" ;
567
- mkdir (base , 0755 );
618
+ make_dir (base , 0755 );
568
619
} else if (!strncmp (uevent -> subsystem , "misc" , 4 ) &&
569
620
!strncmp (name , "log_" , 4 )) {
570
621
base = "/dev/log/" ;
571
- mkdir (base , 0755 );
622
+ make_dir (base , 0755 );
572
623
name += 4 ;
573
624
} else
574
625
base = "/dev/" ;
@@ -819,7 +870,14 @@ void device_init(void)
819
870
suseconds_t t0 , t1 ;
820
871
struct stat info ;
821
872
int fd ;
873
+ #ifdef HAVE_SELINUX
874
+ struct selinux_opt seopts [] = {
875
+ { SELABEL_OPT_PATH , "/file_contexts" }
876
+ };
822
877
878
+ if (is_selinux_enabled () > 0 )
879
+ sehandle = selabel_open (SELABEL_CTX_FILE , seopts , 1 );
880
+ #endif
823
881
/* is 64K enough? udev uses 16MB! */
824
882
device_fd = uevent_open_socket (64 * 1024 , true);
825
883
if (device_fd < 0 )
0 commit comments