jensstein / oandbackup

backup manager for android
Other
543 stars 193 forks source link

Main user SELinux Context applied even on other users #272

Open SebiderSushi opened 4 years ago

SebiderSushi commented 4 years ago

ROM: unofficial build of Resurrection Remix 7.0.2 based on LineageOS 16.0 based on AOSP 9 oandbackup version: 0.3.5-universal (from F-Droid)

Problem

When restoring app data on a secodary device user or work profile (implemented as a special, seperated user account from androids point of view), oandbackup appears to be unable to apply the proper SELinux contexts, thus preventing the restored application from reading its data.

Expected behavior

Current behavior

After following the aforementioned steps, the restored app crashes upon launch. Using a terminal emulator, it is possible to reveal the following:

Running restorecon -R /data/user/11/$packagename on any given app data folder sets the wrong context c512 in my case. As oandbackup probably uses this tool to restore contexts, this looks like the root of the problem. So, is this a bug in my ROM or is it simply incorrect to fully rely on restorecon in all cases?

daniellandau commented 3 years ago

I'm having the same user visible behavior on LineageOS for MicroG 15.1. Didn't yet check file contexts etc, but I'd imagine it's the same. Also trying to restore apps to a work profile.

@SebiderSushi did you find some manual way to fix up things after a restore?

daniellandau commented 3 years ago

For me the fix was

chcon c523,c768 /data/user/11/$packagename

Followed by a reboot

SebiderSushi commented 3 years ago

I don't have a rooted device at hand right now but i think i never tried anything other than restorecon -R "$DIR" to fix stuff (besides manually finding out the right context and calling chcon) so: Does adding the -F option to the restorecon call help? I.E. restorecon -FR /data/user/11/$packagename

Edit: Okay nevermind i pulled out rooted adb on a LineageOS 15.1 and oh boy does running restorecon -DR /data/user/ nuke your secondary user data. I think restorecon only ever applies the default data context c512 all over the place.

One possible workaround i use in a shell script when i restore data in recovery: This records the SELinux context prior to restoring data using ls -Z and re-applies it afterwards.

local ctx="$(ls -Zd "$pkgdir")" &&
ctx="${ctx%% *}" &&
# do restore
chcon -hR "$ctx" "$pkgdir"

(see here for the full app data restore shell script function)

daniellandau commented 3 years ago

For me the manual chcon is enough of a fix for now. Thanks for the shell script though, bookmarked it in case I need it later.