fdcheck: update fdcheck impl

1 store fd in the high position
2 removing the pid information , as the tag information is sufficient.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
This commit is contained in:
hujun5 2024-04-10 09:23:43 +08:00 committed by Xiang Xiao
parent 6c24ff95d4
commit 638716504d
3 changed files with 40 additions and 107 deletions

View File

@ -46,49 +46,47 @@ extern "C"
* *
* Description: Obtain original fd information * Description: Obtain original fd information
* *
* Val carries the pid, tag and fd information. * Val carries the tag and fd information.
* The original fd information is stored in low bit of val. * The original fd information is stored in high bit of val.
* The pid and tag information is stored in the high bit of val. * The tag information is stored in the low bit of val.
* For ease of understanding, let's give an example where * For ease of understanding, let's give an example where
* the following information is represented in 32-bit binary format * the following information is represented in 32-bit binary format
* *
* val 00000000 01010101 00000001 10001010 * val 00000000 00000000 10001010 00000001
* fd 00000000 00000000 00000000 10001010 * fd 00000000 00000000 00000000 10001010
* pid 00000000 00000000 00000000 01010101
* tag 00000000 00000000 00000000 00000001 * tag 00000000 00000000 00000000 00000001
* *
* In this function, we also check if the pid and tag information is correct. * In this function, we also check tag information is correct.
* If there is an error, it will panic. * If there is an error, it will panic.
* *
* Input Parameters: * Input Parameters:
* val - this val carrying pid, tag and original fd information * val - this val carrying tag and original fd information
* *
* Returned Value: none * Returned Value: The original fd is returned.
* *
****************************************************************************/ ****************************************************************************/
int fdcheck_restore(int fd); int fdcheck_restore(int val);
/**************************************************************************** /****************************************************************************
* Name: fdcheck_protect * Name: fdcheck_protect
* *
* Description: Obtain the combined value of fd, pid and tag * Description: Obtain the combined value of fd and tag
* *
* the return value carries the pid, tag and fd information. * the return value carries the tag and fd information.
* The original fd information is stored in low bit of val. * The original fd information is stored in low bit of val.
* The pid and tag information is stored in high bit of val. * The tag information is stored in high bit of val.
* For ease of understanding, let's give an example where * For ease of understanding, let's give an example where
* the following information is represented in 32-bit binary format * the following information is represented in 32-bit binary format
* *
* fd 00000000 00000000 00000000 10001010 * fd 00000000 00000000 00000000 10001010
* pid 00000000 00000000 00000000 01010101
* tag 00000000 00000000 00000000 00000001 * tag 00000000 00000000 00000000 00000001
* val 00000000 01010101 00000001 10001010 * val 00000000 00000000 10001010 00000001
* *
* Input Parameters: * Input Parameters:
* fd - original fd * fd - original fd
* *
* Returned Value: the combined value of fd and pid * Returned Value: the combined value of fd and tag
* *
****************************************************************************/ ****************************************************************************/

View File

@ -37,17 +37,15 @@
* Pre-processor Definitions * Pre-processor Definitions
****************************************************************************/ ****************************************************************************/
#define FD_SHIFT 0 #define TAG_SHIFT 0
#define FD_BITS LOG2_CEIL(OPEN_MAX)
#define FD_MASK ((1 << FD_BITS) - 1)
#define TAG_SHIFT (FD_BITS + FD_SHIFT)
#define TAG_BITS 8 #define TAG_BITS 8
#define TAG_MASK ((1 << TAG_BITS) - 1) #define TAG_MASK ((1 << TAG_BITS) - 1)
#define PID_SHIFT (TAG_BITS + TAG_SHIFT) #define FD_SHIFT (TAG_SHIFT + TAG_BITS)
#define PID_BITS (8 * sizeof(int) - 1 - PID_SHIFT) #define FD_BITS LOG2_CEIL(OPEN_MAX)
#define PID_MASK ((1 << PID_BITS) - 1) #define FD_MASK ((1 << FD_BITS) - 1)
static_assert(FD_BITS <= TAG_BITS, "FD_BITS is too long");
/**************************************************************************** /****************************************************************************
* Private Data * Private Data
@ -60,57 +58,20 @@ static uint8_t g_fdcheck_tag = 0;
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/****************************************************************************
* Name: fdcheck_restore
*
* Description: Obtain original fd information
*
* Val carries the pid, tag and fd information.
* The original fd information is stored in low bit of val.
* The pid and tag information is stored in the high bit of val.
* For ease of understanding, let's give an example where
* the following information is represented in 32-bit binary format
*
* val 00000000 01010101 00000001 10001010
* fd 00000000 00000000 00000000 10001010
* pid 00000000 00000000 00000000 01010101
* tag 00000000 00000000 00000000 00000001
*
* In this function, we also check if the pid and tag information is correct.
* If there is an error, it will panic.
*
* Input Parameters:
* val - this val carrying pid, tag and original fd information
*
* Returned Value: none
*
****************************************************************************/
int fdcheck_restore(int val) int fdcheck_restore(int val)
{ {
int pid_expect; uint8_t tag_store;
int ppid_now; int fd;
int pid_now;
if (val <= 2) /* If val is a bare fd0~255, we should return it directly */
fd = (val >> FD_SHIFT) & FD_MASK;
if (fd == 0 || val < 0)
{ {
return val; return val;
} }
pid_expect = (val >> PID_SHIFT) & PID_MASK; int ret = ioctl(fd, FIOC_GETTAG_FDCHECK, &tag_store);
pid_now = _SCHED_GETPID() & PID_MASK;
ppid_now = _SCHED_GETPPID() & PID_MASK;
if (pid_expect != pid_now && pid_expect != ppid_now && pid_expect != 0)
{
ferr("pid_expect %d pid_now %d ppid_now %d\n",
pid_expect, pid_now, ppid_now);
PANIC();
}
if (pid_expect != 0)
{
uint8_t tag_store;
int ret = ioctl(val & FD_MASK, FIOC_GETTAG_FDCHECK, &tag_store);
if (ret >= 0) if (ret >= 0)
{ {
uint8_t tag_expect = (val >> TAG_SHIFT) & TAG_MASK; uint8_t tag_expect = (val >> TAG_SHIFT) & TAG_MASK;
@ -121,34 +82,10 @@ int fdcheck_restore(int val)
PANIC(); PANIC();
} }
} }
}
return val & FD_MASK; return fd;
} }
/****************************************************************************
* Name: fdcheck_protect
*
* Description: Obtain the combined value of fd, pid and tag
*
* the return value carries the pid, tag and fd information.
* The original fd information is stored in low bit of val.
* The pid and tag information is stored in high bit of val.
* For ease of understanding, let's give an example where
* the following information is represented in 32-bit binary format
*
* fd 00000000 00000000 00000000 10001010
* pid 00000000 00000000 00000000 01010101
* tag 00000000 00000000 00000000 00000001
* val 00000000 01010101 00000001 10001010
*
* Input Parameters:
* fd - original fd
*
* Returned Value: the combined value of fd and pid
*
****************************************************************************/
int fdcheck_protect(int fd) int fdcheck_protect(int fd)
{ {
int protect_fd; int protect_fd;
@ -160,9 +97,7 @@ int fdcheck_protect(int fd)
return fd; return fd;
} }
protect_fd = fd & FD_MASK; protect_fd = (fd & FD_MASK) << FD_SHIFT;
protect_fd |= (_SCHED_GETPID() & PID_MASK) << PID_SHIFT;
ret = ioctl(fd, FIOC_GETTAG_FDCHECK, &tag); ret = ioctl(fd, FIOC_GETTAG_FDCHECK, &tag);
DEBUGASSERT(ret >= 0); DEBUGASSERT(ret >= 0);
if (tag == 0) if (tag == 0)

View File

@ -105,6 +105,12 @@ FAR FILE *fdopen(int fd, FAR const char *mode)
/* Initialize the mutex the manages access to the buffer */ /* Initialize the mutex the manages access to the buffer */
nxrmutex_init(&filep->fs_lock); nxrmutex_init(&filep->fs_lock);
#ifdef CONFIG_FDSAN
android_fdsan_exchange_owner_tag(fd, 0,
android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_FILE,
(uintptr_t)filep));
#endif
} }
else else
{ {
@ -135,12 +141,6 @@ FAR FILE *fdopen(int fd, FAR const char *mode)
filep->fs_cookie = (FAR void *)(intptr_t)fd; filep->fs_cookie = (FAR void *)(intptr_t)fd;
filep->fs_oflags = oflags; filep->fs_oflags = oflags;
#ifdef CONFIG_FDSAN
android_fdsan_exchange_owner_tag(fd, 0,
android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_FILE,
(uintptr_t)filep));
#endif
/* Assign custom callbacks to NULL. */ /* Assign custom callbacks to NULL. */
filep->fs_iofunc.read = NULL; filep->fs_iofunc.read = NULL;