2013-09-10 110 views
2

我試圖使用SecureRandom的解決辦法,谷歌發表在我的Android應用程序: http://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html如何檢測SELinux是否在Android應用程序中啓用?

此解決涉及寫入(和讀取)的/ dev/urandom的。但是,它看起來像三星啓用SELinux的方式,防止應用程序訪問/ dev/urandom。

我沒有這些設備之一,所以對我來說測試解決方案有點困難,除了推出Android市場上的解決方法之外的嘗試,但似乎這不是一個錯誤,我可以用try catch塊捕獲。它也似乎File.canRead和canWrite返回true。您可以在工作區看到我嘗試在supportedOnThisDevice方法在下面的類: PRNGFixes.java

我正在尋找一個可靠的方式,如果我是這樣的設備來檢測,如果是這樣,不適用的谷歌SecureRandom的解決方法。

回答

2

我聽說三星正在將SELinux策略設置爲Enforce的設備出貨,但我不知道它是否正確。據我所知,4.3上的大多數設備仍然設置爲寬容。

根據Google的說法,"SELinux reinforcement is invisible to users and developers, and adds robustness to the existing Android security model while maintaining compatibility with existing applications."因此,您可能需要檢查系統屬性或通過shell測試以確定。

如果你能有人給你他們的build.prop您可以通過通過System.getProperty("ro.build.selinux"), 他們ro.build.selinux屬性比較趕,但你還需要驗證你能訪問它更直接,以防萬一它不可靠或getProperty()在未來的更新中被破壞。

根(SELinux上的系統用戶)是可用時的另一種選擇,但無論哪種方式,基於shell的解決方案可能是您最好的選擇。

1
System.getProperty("ro.build.selinux") 

沒有爲我工作的三星S4 Android 4.3。所以我寫了這個

private static final int JELLY_BEAN_MR2 = 18; 

public static boolean isSELinuxSupported() { 
     // Didnt' work 
     //String selinuxStatus = System.getProperty(PROPERTY_SELINUX_STATUS); 
     //return selinuxStatus.equals("1") ? true : false; 

     String selinuxFlag = getSelinuxFlag(); 

     if (!StringUtils.isEmpty(selinuxFlag)) { 
      return selinuxFlag.equals("1") ? true : false; 
     } else { 
      // 4.3 or later ? 
      if(Build.VERSION.SDK_INT >= JELLY_BEAN_MR2) { 
       return true; 
      } 
      else { 
       return false; 
      } 
     }  
} 

public static String getSelinuxFlag() { 
     String selinux = null; 

     try { 
      Class<?> c = Class.forName("android.os.SystemProperties"); 
      Method get = c.getMethod("get", String.class); 
      selinux = (String) get.invoke(c, "ro.build.selinux"); 
     } catch (Exception ignored) { 
     } 

     return selinux; 
} 
+0

什麼返回值這種方法? – dkneller

+0

isSELinuxSupported返回true(如果存在SELinux)或爲false – user1546570

+0

SELinux在我的一些設備上強制執行,但'ro.build.selinux'沒有值,因此這是不可靠的。 –

3

這是我的檢查方法是SELinux時強制模式 - 可以通過任何shell腳本完成,而不是依賴於RootTools:

private static boolean isSELinuxEnforcing() { 
    try { 
     CommandCapture command = new CommandCapture(1, "getenforce"); 
     RootTools.getShell(false).add(command).waitForFinish(); 
     boolean isSELinuxEnforcing = command.toString().trim().equalsIgnoreCase("enforcing"); 
     return isSELinuxEnforcing; 
    } catch (Exception e) { 
     // handle exception 
    } 
    return false; 
} 
相關問題