Skip to content

Commit c792dba

Browse files
pcercueiQuzarDC
authored andcommitted
fs: Start FD values at 3
The file descriptors with values 0, 1 and 2 are reserved respectively for the standard input, the standard output, and the error output. Having the filesystem code allocate FD values starting from 0 was pretty dangerous; case in point, the PTY driver would open /pty/sl00 and get the file descriptor 0, and doing so, mapping /pty/sl00 to the standard input, even though the PTY code did not explicitly set that up. The simpler fix is just to start the FD values at 3. To avoid changing behaviour, the PTY driver will now also dup2() the opened PTY file to the stdin FD. Signed-off-by: Paul Cercueil <paul@crapouillou.net>
1 parent 35d8e02 commit c792dba

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

kernel/fs/fs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ static int fs_hnd_assign(fs_hnd_t *hnd) {
194194

195195
fs_hnd_ref(hnd);
196196

197-
for(i = 0; i < FD_SETSIZE; i++) {
197+
for(i = 3; i < FD_SETSIZE; i++) {
198198
fs_hnd_t *old = NULL;
199199

200200
if(atomic_compare_exchange_strong(&fd_table[i], &old, hnd))

kernel/fs/fs_pty.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ int fs_pty_create(char *buffer, int maxbuflen, file_t *master_out, file_t *slave
180180

181181
if(boot) {
182182
/* Get the slave channel setup first, and dup it across
183-
our stdout and stderr. */
183+
our stdin, stdout and stderr. */
184+
fs_dup2(*slave_out, STDIN_FILENO);
184185
fs_dup2(*slave_out, STDOUT_FILENO);
185186
fs_dup2(*slave_out, STDERR_FILENO);
186187
}

0 commit comments

Comments
 (0)