-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selinux: Fix Android specific configs handling in policydb_write()
orig patch: https://android-review.googlesource.com/c/kernel/common/+/3009995 Co-authored-by: sekaiacg <[email protected]> Co-authored-by: Wang Han <[email protected]> Signed-off-by: GarfieldHan <[email protected]>
- Loading branch information
1 parent
031bcfd
commit 8fee643
Showing
5 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* SPDX-License-Identifier: GPL-2.0-or-later */ | ||
/* | ||
* Copyright (C) 2024 1f2003d5. All Rights Reserved. | ||
* Copyright (C) 2024 sekaiacg. All Rights Reserved. | ||
*/ | ||
|
||
#include "sepolicy_flags.h" | ||
|
||
#include <ksyms.h> | ||
#include <uapi/scdefs.h> | ||
#include <linux/spinlock.h> | ||
#include <linux/capability.h> | ||
#include <linux/security.h> | ||
#include <asm/current.h> | ||
#include <asm/thread_info.h> | ||
#include <uapi/asm-generic/errno.h> | ||
#include <hook.h> | ||
#include <linux/string.h> | ||
#include <predata.h> | ||
|
||
/* | ||
* see: https://android-review.googlesource.com/c/kernel/common/+/3009995 | ||
* | ||
*/ | ||
|
||
static int (*policydb_write_backup)(struct _policydb *p, struct _policy_file *fp) = 0; | ||
static int policydb_write_replace(struct _policydb *p, struct _policy_file *fp) | ||
{ | ||
char *data = fp->data; | ||
int ret = policydb_write_backup(p, fp); | ||
if (!ret) { | ||
__le32 *config = (__le32 *)(data + POLICYDB_CONFIG_OFFSET); | ||
__le32 before_config = *config; | ||
bool android_netlink_route_exists = before_config & POLICYDB_CONFIG_ANDROID_NETLINK_ROUTE; | ||
bool android_netlink_getneigh_exists = before_config & POLICYDB_CONFIG_ANDROID_NETLINK_GETNEIGH; | ||
if (p->android_netlink_route == 1 && !android_netlink_route_exists) { | ||
*config |= POLICYDB_CONFIG_ANDROID_NETLINK_ROUTE; | ||
} | ||
if (p->android_netlink_getneigh == 1 && !android_netlink_getneigh_exists) { | ||
*config |= POLICYDB_CONFIG_ANDROID_NETLINK_GETNEIGH; | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
int android_sepolicy_flags_init() | ||
{ | ||
unsigned long policydb_write_addr = get_preset_patch_sym()->policydb_write; | ||
if (likely(policydb_write_addr)) { | ||
hook_err_t err = hook((void *)policydb_write_addr, (void *)policydb_write_replace, (void **)&policydb_write_backup); | ||
if (unlikely(err != HOOK_NO_ERR)) { | ||
log_boot("hook policydb_write_addr: %llx, error: %d\n", policydb_write_addr, err); | ||
return -1; | ||
} | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* SPDX-License-Identifier: GPL-2.0-or-later */ | ||
/* | ||
* Copyright (C) 2024 1f2003d5. All Rights Reserved. | ||
* Copyright (C) 2024 sekaiacg. All Rights Reserved. | ||
*/ | ||
|
||
#ifndef _KP_SEPOLICY_FLAGS_H_ | ||
#define _KP_SEPOLICY_FLAGS_H_ | ||
|
||
#include <linux/string.h> | ||
|
||
#define SELINUX_MAGIC 0xf97cff8c | ||
#define POLICYDB_MAGIC SELINUX_MAGIC | ||
#define POLICYDB_STRING "SE Linux" | ||
|
||
#define POLICYDB_CONFIG_MLS 1 | ||
#define POLICYDB_CONFIG_ANDROID_NETLINK_ROUTE (1 << 31) | ||
#define POLICYDB_CONFIG_ANDROID_NETLINK_GETNEIGH (1 << 30) | ||
|
||
/* | ||
* config offset: | ||
* __le32(POLICYDB_MAGIC) + __le32(POLICYDB_STRING_LEN) + | ||
* char[POLICYDB_STRING_LEN] + __le32(policyvers) | ||
*/ | ||
#define POLICYDB_CONFIG_OFFSET (2 * sizeof(__le32) + strlen(POLICYDB_STRING) + sizeof(__le32)) | ||
|
||
struct _policy_file | ||
{ | ||
char *data; | ||
size_t len; | ||
}; | ||
|
||
struct _policydb | ||
{ | ||
int mls_enabled; | ||
int android_netlink_route; | ||
int android_netlink_getneigh; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters