virtmngr: get secontext of the caller app

This patch uses getprevcon from libselinux to get the secontext of the
caller process (i.e. the process that "owns" the VM).

Since virtmngr is started by the "VM owner" process by forking+execing
virtmngr process as a child, we can make use of the getprevcon function
in the libselinux. This function returns the secontext before the last
exec.

In this patch we simply log the callers secontext, in the follow up
patches the callers secontext will be used to check if the VM is allowed
to access the requested secure services.

Bug: 360102915
Test: atest MicrodroidTests
Change-Id: Ia484c6e44efd703ace1eb2f6f72675e153dfa5a4
diff --git a/android/virtmgr/src/selinux.rs b/android/virtmgr/src/selinux.rs
index ba62b7f..284cd23 100644
--- a/android/virtmgr/src/selinux.rs
+++ b/android/virtmgr/src/selinux.rs
@@ -117,3 +117,19 @@
         _ => Err(anyhow!(io::Error::last_os_error())).context("fgetfilecon failed"),
     }
 }
+
+pub fn getprevcon() -> Result<SeContext> {
+    let mut con: *mut c_char = ptr::null_mut();
+    // SAFETY: the returned pointer `con` is wrapped in SeContext::Raw which is freed with
+    // `freecon` when it is dropped.
+    match unsafe { selinux_bindgen::getprevcon(&mut con) } {
+        0.. => {
+            if !con.is_null() {
+                Ok(SeContext::Raw(con))
+            } else {
+                Err(anyhow!("getprevcon returned a NULL context"))
+            }
+        }
+        _ => Err(anyhow!(io::Error::last_os_error())).context("getprevcon failed"),
+    }
+}