static void drop_privileges(
int
server_port) {
ScopedMinijail jail(minijail_new());
gid_t groups[]
=
{AID_ADB, AID_LOG, AID_INPUT, AID_INET,
AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
AID_NET_BW_STATS, AID_READPROC, AID_UHID, AID_EXT_DATA_RW,
AID_EXT_OBB_RW, AID_READTRACEFS};
minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups);
/
/
Don't listen on a port (default
5037
)
if
running
in
secure mode.
/
/
Don't run as root
if
running
in
secure mode.
if
(should_drop_privileges()) {
/
/
判断是否降权 修改false 就行
const
bool
should_drop_caps
=
!__android_log_is_debuggable();
if
(should_drop_caps) {
minijail_use_caps(jail.get(), CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
}
minijail_change_gid(jail.get(), AID_SHELL);
minijail_change_uid(jail.get(), AID_SHELL);
/
/
minijail_enter() will abort
if
any
priv
-
dropping step fails.
minijail_enter(jail.get());
/
/
Whenever ambient capabilities are being used, minijail cannot
/
/
simultaneously drop the bounding capability
set
to just
/
/
CAP_SETUID|CAP_SETGID
while
clearing the inheritable, effective,
/
/
and
permitted sets. So we need to do that
in
two steps.
using ScopedCaps
=
std::unique_ptr<std::remove_pointer<cap_t>::
type
, std::function<void(cap_t)>>;
ScopedCaps caps(cap_get_proc(), &cap_free);
if
(cap_clear_flag(caps.get(), CAP_INHERITABLE)
=
=
-
1
) {
PLOG(FATAL) <<
"cap_clear_flag(INHERITABLE) failed"
;
}
if
(cap_clear_flag(caps.get(), CAP_EFFECTIVE)
=
=
-
1
) {
PLOG(FATAL) <<
"cap_clear_flag(PEMITTED) failed"
;
}
if
(cap_clear_flag(caps.get(), CAP_PERMITTED)
=
=
-
1
) {
PLOG(FATAL) <<
"cap_clear_flag(PEMITTED) failed"
;
}
if
(cap_set_proc(caps.get()) !
=
0
) {
PLOG(FATAL) <<
"cap_set_proc() failed"
;
}
D(
"Local port disabled"
);
}
else
{
/
/
minijail_enter() will abort
if
any
priv
-
dropping step fails.
minijail_enter(jail.get());
if
(root_seclabel !
=
nullptr) {
if
(selinux_android_setcon(root_seclabel) <
0
) {
/
/
If we failed to become root, don't
try
again to avoid a
/
/
restart loop.
android::base::SetProperty(
"service.adb.root"
,
"0"
);
LOG(FATAL) <<
"Could not set SELinux context"
;
}
}
}
}