Skip to content

Commit 67fa155

Browse files
authored
libc-wasi: Make rights of STDIN/STDOUT/STDERR fixed and overlook their access modes (#3694)
When determining the file descriptor rights in the function fd_determine_type_rights(), we assign fixed and unchangeable rights to STDIN, STDOUT and STDERR. ps. #3686
1 parent 140ff25 commit 67fa155

File tree

6 files changed

+138
-19
lines changed

6 files changed

+138
-19
lines changed

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,27 @@ fd_determine_type_rights(os_file_handle fd, __wasi_filetype_t *type,
459459
__wasi_rights_t *rights_inheriting)
460460
{
461461
struct __wasi_filestat_t buf;
462-
__wasi_errno_t error = os_fstat(fd, &buf);
462+
__wasi_errno_t error;
463+
464+
if (os_is_stdin_handle(fd)) {
465+
*rights_base = RIGHTS_STDIN;
466+
*rights_inheriting = RIGHTS_STDIN;
467+
return __WASI_ESUCCESS;
468+
}
469+
470+
if (os_is_stdout_handle(fd)) {
471+
*rights_base = RIGHTS_STDOUT;
472+
*rights_inheriting = RIGHTS_STDOUT;
473+
return __WASI_ESUCCESS;
474+
}
475+
476+
if (os_is_stderr_handle(fd)) {
477+
*rights_base = RIGHTS_STDERR;
478+
*rights_inheriting = RIGHTS_STDERR;
479+
return __WASI_ESUCCESS;
480+
}
463481

482+
error = os_fstat(fd, &buf);
464483
if (error != __WASI_ESUCCESS)
465484
return error;
466485

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/rights.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@
4747
#define RIGHTS_CHARACTER_DEVICE_BASE RIGHTS_ALL
4848
#define RIGHTS_CHARACTER_DEVICE_INHERITING RIGHTS_ALL
4949

50+
#define RIGHTS_STDIN \
51+
(__WASI_RIGHT_FD_ADVISE | __WASI_RIGHT_FD_FILESTAT_GET | \
52+
__WASI_RIGHT_FD_READ | __WASI_RIGHT_FD_WRITE | \
53+
__WASI_RIGHT_POLL_FD_READWRITE)
54+
55+
#define RIGHTS_STDOUT \
56+
(__WASI_RIGHT_FD_ADVISE | __WASI_RIGHT_FD_DATASYNC | \
57+
__WASI_RIGHT_FD_FILESTAT_GET | __WASI_RIGHT_FD_SYNC | \
58+
__WASI_RIGHT_FD_READ | __WASI_RIGHT_FD_WRITE | \
59+
__WASI_RIGHT_POLL_FD_READWRITE)
60+
61+
#define RIGHTS_STDERR RIGHTS_STDOUT
62+
5063
// Only allow directory operations on directories. Directories can only
5164
// yield file descriptors to other directories and files.
5265
#define RIGHTS_DIRECTORY_BASE \

core/shared/platform/common/posix/posix_file.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@
5454
#define CONFIG_HAS_O_SYNC
5555
#endif
5656

57+
#ifndef STDIN_FILENO
58+
#define STDIN_FILENO 0
59+
#endif
60+
61+
#ifndef STDOUT_FILENO
62+
#define STDOUT_FILENO 1
63+
#endif
64+
65+
#ifndef STDERR_FILENO
66+
#define STDERR_FILENO 2
67+
#endif
68+
5769
// Converts a POSIX timespec to a WASI timestamp.
5870
static __wasi_timestamp_t
5971
convert_timespec(const struct timespec *ts)
@@ -858,30 +870,39 @@ os_isatty(os_file_handle handle)
858870
#endif
859871
}
860872

873+
bool
874+
os_is_stdin_handle(os_file_handle fd)
875+
{
876+
return fd == STDIN_FILENO;
877+
}
878+
879+
bool
880+
os_is_stdout_handle(os_file_handle fd)
881+
{
882+
return fd == STDOUT_FILENO;
883+
}
884+
885+
bool
886+
os_is_stderr_handle(os_file_handle fd)
887+
{
888+
return fd == STDERR_FILENO;
889+
}
890+
861891
os_file_handle
862892
os_convert_stdin_handle(os_raw_file_handle raw_stdin)
863893
{
864-
#ifndef STDIN_FILENO
865-
#define STDIN_FILENO 0
866-
#endif
867894
return raw_stdin >= 0 ? raw_stdin : STDIN_FILENO;
868895
}
869896

870897
os_file_handle
871898
os_convert_stdout_handle(os_raw_file_handle raw_stdout)
872899
{
873-
#ifndef STDOUT_FILENO
874-
#define STDOUT_FILENO 1
875-
#endif
876900
return raw_stdout >= 0 ? raw_stdout : STDOUT_FILENO;
877901
}
878902

879903
os_file_handle
880904
os_convert_stderr_handle(os_raw_file_handle raw_stderr)
881905
{
882-
#ifndef STDERR_FILENO
883-
#define STDERR_FILENO 2
884-
#endif
885906
return raw_stderr >= 0 ? raw_stderr : STDERR_FILENO;
886907
}
887908

core/shared/platform/esp-idf/espidf_file.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@
5454
#define CONFIG_HAS_O_SYNC
5555
#endif
5656

57+
#ifndef STDIN_FILENO
58+
#define STDIN_FILENO 0
59+
#endif
60+
61+
#ifndef STDOUT_FILENO
62+
#define STDOUT_FILENO 1
63+
#endif
64+
65+
#ifndef STDERR_FILENO
66+
#define STDERR_FILENO 2
67+
#endif
68+
5769
// Converts a POSIX timespec to a WASI timestamp.
5870
static __wasi_timestamp_t
5971
convert_timespec(const struct timespec *ts)
@@ -858,30 +870,39 @@ os_isatty(os_file_handle handle)
858870
#endif
859871
}
860872

873+
bool
874+
os_is_stdin_handle(os_file_handle fd)
875+
{
876+
return fd == STDIN_FILENO;
877+
}
878+
879+
bool
880+
os_is_stdout_handle(os_file_handle fd)
881+
{
882+
return fd == STDOUT_FILENO;
883+
}
884+
885+
bool
886+
os_is_stderr_handle(os_file_handle fd)
887+
{
888+
return fd == STDERR_FILENO;
889+
}
890+
861891
os_file_handle
862892
os_convert_stdin_handle(os_raw_file_handle raw_stdin)
863893
{
864-
#ifndef STDIN_FILENO
865-
#define STDIN_FILENO 0
866-
#endif
867894
return raw_stdin >= 0 ? raw_stdin : STDIN_FILENO;
868895
}
869896

870897
os_file_handle
871898
os_convert_stdout_handle(os_raw_file_handle raw_stdout)
872899
{
873-
#ifndef STDOUT_FILENO
874-
#define STDOUT_FILENO 1
875-
#endif
876900
return raw_stdout >= 0 ? raw_stdout : STDOUT_FILENO;
877901
}
878902

879903
os_file_handle
880904
os_convert_stderr_handle(os_raw_file_handle raw_stderr)
881905
{
882-
#ifndef STDERR_FILENO
883-
#define STDERR_FILENO 2
884-
#endif
885906
return raw_stderr >= 0 ? raw_stderr : STDERR_FILENO;
886907
}
887908

core/shared/platform/include/platform_api_extension.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,33 @@ os_convert_stdout_handle(os_raw_file_handle raw_stdout);
15021502
os_file_handle
15031503
os_convert_stderr_handle(os_raw_file_handle raw_stderr);
15041504

1505+
/**
1506+
*
1507+
* @param fd a file handle
1508+
*
1509+
* @return true if it is stdin
1510+
*/
1511+
bool
1512+
os_is_stdin_handle(os_file_handle fd);
1513+
1514+
/**
1515+
*
1516+
* @param fd a file handle
1517+
*
1518+
* @return true if it is stdout
1519+
*/
1520+
bool
1521+
os_is_stdout_handle(os_file_handle fd);
1522+
1523+
/**
1524+
*
1525+
* @param fd a file handle
1526+
*
1527+
* @return true if it is stderr
1528+
*/
1529+
bool
1530+
os_is_stderr_handle(os_file_handle fd);
1531+
15051532
/**
15061533
* Open a directory stream for the provided directory handle. The returned
15071534
* directory stream will be positioned at the first entry in the directory.

core/shared/platform/windows/win_file.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,24 @@ create_stdio_handle(HANDLE raw_stdio_handle, DWORD stdio)
15401540
return stdio_handle;
15411541
}
15421542

1543+
bool
1544+
os_is_stdin_handle(os_file_handle fd)
1545+
{
1546+
return fd->raw.handle == GetStdHandle(STD_INPUT_HANDLE);
1547+
}
1548+
1549+
bool
1550+
os_is_stdout_handle(os_file_handle fd)
1551+
{
1552+
return fd->raw.handle == GetStdHandle(STD_OUTPUT_HANDLE);
1553+
}
1554+
1555+
bool
1556+
os_is_stderr_handle(os_file_handle fd)
1557+
{
1558+
return fd->raw.handle == GetStdHandle(STD_ERROR_HANDLE);
1559+
}
1560+
15431561
os_file_handle
15441562
os_convert_stdin_handle(os_raw_file_handle raw_stdin)
15451563
{

0 commit comments

Comments
 (0)