3838#include <cutils/log.h>
3939
4040#include "su.h"
41+ #include "utils.h"
4142
4243/* Still lazt, will fix this */
4344static char socket_path [PATH_MAX ];
@@ -375,8 +376,10 @@ int main(int argc, char *argv[])
375376 };
376377 struct stat st ;
377378 int socket_serv_fd , fd ;
378- char buf [64 ], * result ;
379- int c , dballow ;
379+ char buf [64 ], * result , debuggable [PROPERTY_VALUE_MAX ];
380+ char enabled [PROPERTY_VALUE_MAX ], build_type [PROPERTY_VALUE_MAX ];
381+ char cm_version [PROPERTY_VALUE_MAX ];;
382+ int c , dballow , len ;
380383 struct option long_opts [] = {
381384 { "command" , required_argument , NULL , 'c' },
382385 { "help" , no_argument , NULL , 'h' },
@@ -386,6 +389,8 @@ int main(int argc, char *argv[])
386389 { "version" , no_argument , NULL , 'v' },
387390 { NULL , 0 , NULL , 0 },
388391 };
392+ char * data ;
393+ unsigned sz ;
389394
390395 while ((c = getopt_long (argc , argv , "+c:hlmps:Vv" , long_opts , NULL )) != -1 ) {
391396 switch (c ) {
@@ -450,8 +455,52 @@ int main(int argc, char *argv[])
450455 deny (& ctx );
451456 }
452457
458+ // we can't simply use the property service, since we aren't launched from init and
459+ // can't trust the location of the property workspace. find the properties ourselves.
460+ data = read_file ("/default.prop" , & sz );
461+ get_property (data , debuggable , "ro.debuggable" , "0" );
462+ free (data );
463+
464+ data = read_file ("/system/build.prop" , & sz );
465+ get_property (data , cm_version , "ro.cm.version" , "" );
466+ get_property (data , build_type , "ro.build.type" , "" );
467+ free (data );
468+
469+ data = read_file ("/data/property/persist.sys.root_access" , & sz );
470+ if (data != NULL ) {
471+ len = strlen (data );
472+ if (len >= PROPERTY_VALUE_MAX )
473+ memcpy (enabled , "1" , 2 );
474+ else
475+ memcpy (enabled , data , len + 1 );
476+ free (data );
477+ } else
478+ memcpy (enabled , "1" , 2 );
479+
453480 ctx .umask = umask (027 );
454481
482+ // CyanogenMod-specific behavior
483+ if (strlen (cm_version ) > 0 ) {
484+ // only allow su on debuggable builds
485+ if (strcmp ("1" , debuggable ) != 0 ) {
486+ LOGE ("Root access is disabled on non-debug builds" );
487+ deny (& ctx );
488+ }
489+
490+ // enforce persist.sys.root_access on non-eng builds
491+ if (strcmp ("eng" , build_type ) != 0 &&
492+ (atoi (enabled ) & 1 ) != 1 ) {
493+ LOGE ("Root access is disabled by system setting - enable it under settings -> developer options" );
494+ deny (& ctx );
495+ }
496+
497+ // disallow su in a shell if appropriate
498+ if (ctx .from .uid == AID_SHELL && (atoi (enabled ) == 1 )) {
499+ LOGE ("Root access is disabled by a system setting - enable it under settings -> developer options" );
500+ deny (& ctx );
501+ }
502+ }
503+
455504 if (ctx .from .uid == AID_ROOT || ctx .from .uid == AID_SHELL )
456505 allow (& ctx );
457506
0 commit comments