mirror of
https://github.com/apache/nuttx.git
synced 2025-01-13 08:38:38 +08:00
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:
parent
6c24ff95d4
commit
638716504d
3 changed files with 40 additions and 107 deletions
|
@ -46,49 +46,47 @@ extern "C"
|
|||
*
|
||||
* 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.
|
||||
* Val carries the tag and fd information.
|
||||
* The original fd information is stored in 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
|
||||
* 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
|
||||
* pid 00000000 00000000 00000000 01010101
|
||||
* 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.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* 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 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
|
||||
* 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
|
||||
* val 00000000 00000000 10001010 00000001
|
||||
*
|
||||
* Input Parameters:
|
||||
* fd - original fd
|
||||
*
|
||||
* Returned Value: the combined value of fd and pid
|
||||
* Returned Value: the combined value of fd and tag
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
|
|
|
@ -37,17 +37,15 @@
|
|||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define FD_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_SHIFT 0
|
||||
#define TAG_BITS 8
|
||||
#define TAG_MASK ((1 << TAG_BITS) - 1)
|
||||
|
||||
#define PID_SHIFT (TAG_BITS + TAG_SHIFT)
|
||||
#define PID_BITS (8 * sizeof(int) - 1 - PID_SHIFT)
|
||||
#define PID_MASK ((1 << PID_BITS) - 1)
|
||||
#define FD_SHIFT (TAG_SHIFT + TAG_BITS)
|
||||
#define FD_BITS LOG2_CEIL(OPEN_MAX)
|
||||
#define FD_MASK ((1 << FD_BITS) - 1)
|
||||
|
||||
static_assert(FD_BITS <= TAG_BITS, "FD_BITS is too long");
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
|
@ -60,95 +58,34 @@ static uint8_t g_fdcheck_tag = 0;
|
|||
* 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 pid_expect;
|
||||
int ppid_now;
|
||||
int pid_now;
|
||||
uint8_t tag_store;
|
||||
int fd;
|
||||
|
||||
if (val <= 2)
|
||||
/* If val is a bare fd(0~255), we should return it directly */
|
||||
|
||||
fd = (val >> FD_SHIFT) & FD_MASK;
|
||||
if (fd == 0 || val < 0)
|
||||
{
|
||||
return val;
|
||||
}
|
||||
|
||||
pid_expect = (val >> PID_SHIFT) & PID_MASK;
|
||||
pid_now = _SCHED_GETPID() & PID_MASK;
|
||||
ppid_now = _SCHED_GETPPID() & PID_MASK;
|
||||
if (pid_expect != pid_now && pid_expect != ppid_now && pid_expect != 0)
|
||||
int ret = ioctl(fd, FIOC_GETTAG_FDCHECK, &tag_store);
|
||||
if (ret >= 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)
|
||||
uint8_t tag_expect = (val >> TAG_SHIFT) & TAG_MASK;
|
||||
if (tag_expect != tag_store)
|
||||
{
|
||||
uint8_t tag_expect = (val >> TAG_SHIFT) & TAG_MASK;
|
||||
if (tag_expect != tag_store)
|
||||
{
|
||||
ferr("tag_expect 0x%x tag_store 0x%x\n",
|
||||
tag_expect, tag_store);
|
||||
PANIC();
|
||||
}
|
||||
ferr("tag_expect 0x%x tag_store 0x%x\n",
|
||||
tag_expect, tag_store);
|
||||
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 protect_fd;
|
||||
|
@ -160,9 +97,7 @@ int fdcheck_protect(int fd)
|
|||
return fd;
|
||||
}
|
||||
|
||||
protect_fd = fd & FD_MASK;
|
||||
protect_fd |= (_SCHED_GETPID() & PID_MASK) << PID_SHIFT;
|
||||
|
||||
protect_fd = (fd & FD_MASK) << FD_SHIFT;
|
||||
ret = ioctl(fd, FIOC_GETTAG_FDCHECK, &tag);
|
||||
DEBUGASSERT(ret >= 0);
|
||||
if (tag == 0)
|
||||
|
|
|
@ -105,6 +105,12 @@ FAR FILE *fdopen(int fd, FAR const char *mode)
|
|||
/* Initialize the mutex the manages access to the buffer */
|
||||
|
||||
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
|
||||
{
|
||||
|
@ -135,12 +141,6 @@ FAR FILE *fdopen(int fd, FAR const char *mode)
|
|||
filep->fs_cookie = (FAR void *)(intptr_t)fd;
|
||||
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. */
|
||||
|
||||
filep->fs_iofunc.read = NULL;
|
||||
|
|
Loading…
Reference in a new issue