From dd958e5a21ca6a744432902c479ce827b72eedde Mon Sep 17 00:00:00 2001
From: Chia-I Wu <olv@google.com>
Date: Wed, 12 Oct 2016 06:38:00 -0700
Subject: [PATCH] Add sepolicy for gralloc-alloc HAL

Allow SurfaceFlinger to call into IAllocator, and allow everyone to access
IAllocator's fd.

Specifically,

hwbinder_use(...) for
avc: denied { call } for scontext=u:r:hal_graphics_allocator:s0 tcontext=u:r:hwservicemanager:s0 tclass=binder permissive=1
avc: denied { transfer } for scontext=u:r:hal_graphics_allocator:s0 tcontext=u:r:hwservicemanager:s0 tclass=binder permissive=1

allow ... ion_device:chr_file r_file_perms for
avc: denied { read } for name="ion" dev="tmpfs" ino=15014 scontext=u:r:hal_graphics_allocator:s0 tcontext=u:object_r:ion_device:s0 tclass=chr_file permissive=1
avc: denied { open } for path="/dev/ion" dev="tmpfs" ino=15014 scontext=u:r:hal_graphics_allocator:s0 tcontext=u:object_r:ion_device:s0 tclass=chr_file permissive=1
avc: denied { ioctl } for path="/dev/ion" dev="tmpfs" ino=15014 ioctlcmd=4900 scontext=u:r:hal_graphics_allocator:s0 tcontext=u:object_r:ion_device:s0 tclass=chr_file permissive=1

allow ... gpu_device:chr_file rw_file_perms; for
avc: denied { read write } for name="kgsl-3d0" dev="tmpfs" ino=14956 scontext=u:r:hal_graphics_allocator:s0 tcontext=u:object_r:gpu_device:s0 tclass=chr_file permissive=1
avc: denied { open } for path="/dev/kgsl-3d0" dev="tmpfs" ino=14956 scontext=u:r:hal_graphics_allocator:s0 tcontext=u:object_r:gpu_device:s0 tclass=chr_file permissive=1
avc: denied { ioctl } for path="/dev/kgsl-3d0" dev="tmpfs" ino=14956 ioctlcmd=940 scontext=u:r:hal_graphics_allocator:s0 tcontext=u:object_r:gpu_device:s0 tclass=chr_file permissive=1

binder_call(surfaceflinger, ...) for
avc: denied { call } for scontext=u:r:surfaceflinger:s0 tcontext=u:r:hal_graphics_allocator:s0 tclass=binder permissive=1

allow ... ...:fd use for
avc: denied { use } for path="anon_inode:dmabuf" dev="anon_inodefs" ino=12794 scontext=u:r:surfaceflinger:s0 tcontext=u:r:hal_graphics_allocator:s0 tclass=fd permissive=1

Bug: 32021161
Test: make bootimage
Change-Id: Ie7700142313407ac438c43dd1a85544dc4c67f13
---
 private/file_contexts             |  1 +
 private/hal_graphics_allocator.te |  1 +
 public/app.te                     |  2 ++
 public/bootanim.te                |  1 +
 public/cameraserver.te            |  1 +
 public/hal_graphics_allocator.te  | 10 ++++++++++
 public/mediacodec.te              |  1 +
 public/mediadrmserver.te          |  1 +
 public/mediaserver.te             |  2 ++
 public/surfaceflinger.te          |  4 ++++
 public/system_server.te           |  1 +
 11 files changed, 25 insertions(+)
 create mode 100644 private/hal_graphics_allocator.te
 create mode 100644 public/hal_graphics_allocator.te

diff --git a/private/file_contexts b/private/file_contexts
index e0018e364..382a20d29 100644
--- a/private/file_contexts
+++ b/private/file_contexts
@@ -225,6 +225,7 @@
 /system/lib(64)?/libart.*        u:object_r:libart_file:s0
 /system/bin/hw/android\.hardware\.audio@2\.0-service          u:object_r:hal_audio_exec:s0
 /system/bin/hw/android\.hardware\.boot@1\.0-service           u:object_r:hal_boot_exec:s0
+/system/bin/hw/android\.hardware\.graphics\.allocator@2\.0-service   u:object_r:hal_graphics_allocator_exec:s0
 /system/bin/hw/android\.hardware\.light@2\.0-service          u:object_r:hal_light_exec:s0
 /system/bin/hw/android\.hardware\.memtrack@1\.0-service       u:object_r:hal_memtrack_exec:s0
 /system/bin/hw/android\.hardware\.nfc@1\.0-service            u:object_r:hal_nfc_exec:s0
diff --git a/private/hal_graphics_allocator.te b/private/hal_graphics_allocator.te
new file mode 100644
index 000000000..d00cf68b4
--- /dev/null
+++ b/private/hal_graphics_allocator.te
@@ -0,0 +1 @@
+init_daemon_domain(hal_graphics_allocator)
diff --git a/public/app.te b/public/app.te
index 985e358ac..fe9ad52ee 100644
--- a/public/app.te
+++ b/public/app.te
@@ -243,6 +243,8 @@ auditallow appdomain ion_device:chr_file { write append };
 # TODO audit ion ioctl usage by apps
 auditallow appdomain ion_device:chr_file ioctl;
 
+allow { appdomain -isolated_app } hal_graphics_allocator:fd use;
+
 # TODO: switch to meminfo service
 allow appdomain proc_meminfo:file r_file_perms;
 
diff --git a/public/bootanim.te b/public/bootanim.te
index abbcacac7..4d16f551a 100644
--- a/public/bootanim.te
+++ b/public/bootanim.te
@@ -20,6 +20,7 @@ allow bootanim surfaceflinger_service:service_manager find;
 
 # Allow access to ion memory allocation device
 allow bootanim ion_device:chr_file rw_file_perms;
+allow bootanim hal_graphics_allocator:fd use;
 
 # Read access to pseudo filesystems.
 r_dir_file(bootanim, proc)
diff --git a/public/cameraserver.te b/public/cameraserver.te
index b12d17cc4..51f96d748 100644
--- a/public/cameraserver.te
+++ b/public/cameraserver.te
@@ -15,6 +15,7 @@ allow cameraserver video_device:dir r_dir_perms;
 allow cameraserver video_device:chr_file rw_file_perms;
 allow cameraserver camera_device:chr_file rw_file_perms;
 allow cameraserver ion_device:chr_file rw_file_perms;
+allow cameraserver hal_graphics_allocator:fd use;
 
 allow cameraserver appops_service:service_manager find;
 allow cameraserver audioserver_service:service_manager find;
diff --git a/public/hal_graphics_allocator.te b/public/hal_graphics_allocator.te
new file mode 100644
index 000000000..8b33f9325
--- /dev/null
+++ b/public/hal_graphics_allocator.te
@@ -0,0 +1,10 @@
+# graphics buffer allocator subsystem
+type hal_graphics_allocator, domain;
+type hal_graphics_allocator_exec, exec_type, file_type;
+
+# hwbinder access
+hwbinder_use(hal_graphics_allocator)
+
+# GPU device access
+allow hal_graphics_allocator gpu_device:chr_file rw_file_perms;
+allow hal_graphics_allocator ion_device:chr_file r_file_perms;
diff --git a/public/mediacodec.te b/public/mediacodec.te
index 0b562c12e..a1d90a008 100644
--- a/public/mediacodec.te
+++ b/public/mediacodec.te
@@ -15,6 +15,7 @@ allow mediacodec gpu_device:chr_file rw_file_perms;
 allow mediacodec video_device:chr_file rw_file_perms;
 allow mediacodec video_device:dir search;
 allow mediacodec ion_device:chr_file rw_file_perms;
+allow mediacodec hal_graphics_allocator:fd use;
 
 ###
 ### neverallow rules
diff --git a/public/mediadrmserver.te b/public/mediadrmserver.te
index db8e082f3..ba4fc9bab 100644
--- a/public/mediadrmserver.te
+++ b/public/mediadrmserver.te
@@ -30,6 +30,7 @@ allow mediadrmserver cgroup:file w_file_perms;
 
 # Allow access to ion memory allocation device
 allow mediadrmserver ion_device:chr_file rw_file_perms;
+allow mediadrmserver hal_graphics_allocator:fd use;
 
 # Allow access to app_data and media_data_files
 allow mediadrmserver media_data_file:dir create_dir_perms;
diff --git a/public/mediaserver.te b/public/mediaserver.te
index 94566e0b5..249f63fbe 100644
--- a/public/mediaserver.te
+++ b/public/mediaserver.te
@@ -124,6 +124,8 @@ allow mediaserver media_rw_data_file:file create_file_perms;
 allow mediaserver preloads_data_file:file { getattr read ioctl };
 
 allow mediaserver ion_device:chr_file r_file_perms;
+allow mediaserver hal_graphics_allocator:fd use;
+
 allow mediaserver system_server:fd use;
 
 ###
diff --git a/public/surfaceflinger.te b/public/surfaceflinger.te
index 2afb7d188..4f9f8efaf 100644
--- a/public/surfaceflinger.te
+++ b/public/surfaceflinger.te
@@ -4,6 +4,10 @@ type surfaceflinger_exec, exec_type, file_type;
 
 typeattribute surfaceflinger mlstrustedsubject;
 
+# Perform HwBinder IPC.
+hwbinder_use(surfaceflinger)
+binder_call(surfaceflinger, hal_graphics_allocator)
+
 # Perform Binder IPC.
 binder_use(surfaceflinger)
 binder_call(surfaceflinger, binderservicedomain)
diff --git a/public/system_server.te b/public/system_server.te
index 7b2b1b91b..06ec624c1 100644
--- a/public/system_server.te
+++ b/public/system_server.te
@@ -567,6 +567,7 @@ allow system_server preloads_data_file:dir { r_dir_perms write remove_name rmdir
 
 r_dir_file(system_server, cgroup)
 allow system_server ion_device:chr_file r_file_perms;
+allow system_server hal_graphics_allocator:fd use;
 
 r_dir_file(system_server, proc)
 r_dir_file(system_server, proc_meminfo)
-- 
GitLab