File.mkdir

Blocks creating directories outside of a whitelist of acceptable directories. This should prevent apps from creating directories outside of app-specific locations.

CollectionFif
GroupPublic.Storage
NameFile.mkdir
AuthorFif_
Version2
Updated (UTC)2018-02-20 22:59:55
Created (UTC)2018-02-20 22:59:55
Downloads1260
Class namejava.io.File
Method namemkdir
Parameter types
Return typeboolean
Min SDK1
Max SDK999
Min APK0
Max APK2147483647
Excluded packages-
EnabledYes
OptionalNo
UsageYes
NotifyNo
Settings-
-- Fif.File.mkdir is a Lua hook designed to work with XPrivacyLua.
-- Copyright (C) 2018 Philippe Troin (Fif_ on XDA)

-- Fif.File.mkdir is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.

-- Fif.File.mkdir is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.

-- You should have received a copy of the GNU General Public License
-- along with XPrivacyLua.  If not, see <http://www.gnu.org/licenses/>.

function before(hook, param)
    local clsFile = luajava.bindClass('java.io.File')
    local found = false
    local obj = param:getThis()
    local path = obj.path
    if path == nil then
        found = true
    end

    if not found then
        if luajava.new(clsFile, path):exists() then
            found = true
        end
    end

    if not found then
        local context = param:getApplicationContext()
        local ai = context:getApplicationInfo()
        local clsEnvironment = luajava.bindClass('android.os.Environment')
        local packageName = context:getPackageName()
        local prefixes = {
            '/data/data/' .. packageName,
            ai.dataDir,
            luajava.new(clsFile, ai.sourceDir):getParent(),       
        }

        if ai.deviceProtectedDataDir ~= nil then
            table.insert(prefixes, ai.deviceProtectedDataDir)
        end

        local clsArray = luajava.bindClass('java.lang.reflect.Array')
        local pathJarray = clsEnvironment:buildExternalStorageAppFilesDirs(packageName)
        local i
        for i = 0, pathJarray.length-1 do
            table.insert(prefixes, clsArray:get(pathJarray, i):getParent())
        end

        pathJarray = clsEnvironment:buildExternalStorageAppMediaDirs(packageName)
        for i = 0, pathJarray.length-1 do
            table.insert(prefixes, clsArray:get(pathJarray, i):getAbsolutePath())
        end

        pathJarray = clsEnvironment:buildExternalStorageAppObbDirs(packageName)
        for i = 0, pathJarray.length-1 do
            table.insert(prefixes, clsArray:get(pathJarray, i):getAbsolutePath())
        end

        local prefix
        for i, prefix in pairs(prefixes) do
            if path == prefix or string.sub(path, 1, string.len(prefix)+1) == prefix .. '/' then
                found = true
                break
            end
        end
    end

    local prefixDescr = ''
    if prefixes ~= nil then
       prefixDescr = ' in whitelist: ' .. table.concat(prefixes, ', ')
    end

    if found then
        log('Allow ' .. path .. prefixDescr)
        return false
    else
        log('Deny ' .. path .. prefixDescr)
        param:setResult(false)
        return true, path
    end
end