Commit Graph

42 Commits

Author SHA1 Message Date
Jonas 'Sortie' Termansen 3c43f71084 Implement file descriptor passing.
This change refactors the Unix socket / pipe backend to have a ring buffer
containing segments, where each segment has an optional leading ancillary
buffer containing control messages followed by a normal data buffer.

The SCM_RIGHTS control message has been implemented which transfers file
descriptors to the receiving process. File descriptors are reference counted
and cycles are prevented using the following restrictions:

1) Unix sockets cannot be sent on themselves (on either end).
2) Unix sockets themselves being sent cannot be sent on.
3) Unix sockets cannot send a Unix socket being sent on.

This is a compatible ABI change.
2021-12-31 22:24:11 +01:00
Jonas 'Sortie' Termansen b9898086c6 Add file descriptor table reservations.
The file descriptor table now allows reserving room for multiple file
descriptors without assigning their numbers. This functionality means
any error conditions happen up front and the subsequent number
assignment will never fail.

This change uses the new functionality to fix troublesome error handling
when allocating multiple file descriptors. One pty allocation error path
was even wrong.

There were subtle race conditions where one (kernel) thread may have
allocated one file descriptor, and another thread spuciously replaces it
with something else, and then the second file descriptor allocation
failed in the first thread, and it closes the first file descriptor now
pointing to a different file description. This case seems harmless but
it's not a great class of bugs to exist in the first place. The new
behavior means the file descriptions appear in the file descriptor table
without fail and never needs to be cleaned up midway and is certainly
immune to shenangians from other threads.

Reviewed-by: Pedro Falcato <pedro.falcato@gmail.com>
2021-12-31 22:24:07 +01:00
Jonas 'Sortie' Termansen c25f36cd53 Fix faccessat(2) not supporting root access. 2021-02-17 23:11:31 +01:00
Jonas 'Sortie' Termansen b3e9865e53 Handle SOCK_NONBLOCK at the file descriptor level. 2018-12-08 22:34:50 +01:00
Jonas 'Sortie' Termansen d3a64e1163 Fix chmod(2), chown(2), and utimens(2) opening the path for writing.
The change 9d29e96c3b "Fix open(2) allowing
opening directories invalidly and check O_TRUNC errors." broke the chmod(2),
chown(2), and utimens(2) system calls on directories, because they can no
longer be opened for writing.

This changes fixes the regression by opening such paths for reading. There
is currently no filesystem permission checks for those system calls. However,
those system calls should check the permissions at the time of the operation
rather than relying on the file having been opened for writing previously.
2018-09-01 17:24:40 +02:00
Jonas 'Sortie' Termansen 568c97c77f Fix SEEK_END, file offset overflow, and read/write/mkpartition syscall bugs.
Fix SEEK_END seeking twice as far as requested. Centralize lseek handling in
one place and avoid overflow bugs. Inode lseek handlers now only need to
handle SEEK_END with offset 0. Prevent the file offset from ever going below
zero or overflowing.

Character devices are now not seekable, but lseek will pretend they are, yet
always stay at the file offset 0. pread/pwrite on character devices will now
ignore the file offset and call read/write.

This change prevents character devices from being memory mapped, notably
/dev/zero can no longer be memory mapped. None of the current ports seem
to rely on this behavior and will work with just MAP_ANONYMOUS.

Refactor read and write system calls to have a shared return statement for
both seekable and non-seekable IO.

Fix file offset overflow bugs in read and write system calls.

Fix system calls returning EPERM instead of properly returning EBADF when
the file has not been opened in the right mode.

Truncate IO counts and total vector IO length so the IO operation does not
do any IO beyond OFF_MAX. Truncate also total vector IO length for recvmsg
and sendmsg. Fail with EINVAL if total vector IO length exceeds SSIZE_MAX.

Don't stop early if the total IO length is zero, so zero length IO now block
on any locks internal to the inode.

Handle reads at the maximum file offset with an end of file condition and
handle writes of at least one byte at the maximum file offset by failing
with EFBIG.

Refactor UtilMemoryBuffer to store the file size using off_t instead of
size_t to avoid casts and keep file sizes in the off_t type. Properly
handle errors in the code, such as failing with EROFS instead of EBADF if
the backing memory is not writeable, and failing with EFBIG if writing
beyond the end of the file.

Fix mkpartition not rejecting invalid partition start offsets and lengths.
Strictly enforce partition start and length checks in the partition code.
Enforce partitions exist within regular files or block devices.

Fix a few indention issues.
2017-12-04 23:56:46 +01:00
Jonas 'Sortie' Termansen ef2e478607 Implement getpeername(2) and getsockname(2). 2017-02-26 22:24:35 +01:00
Jonas 'Sortie' Termansen 4eb9caaa39 Fix non-blocking accept4(2) and getting the Unix socket peer address.
Rename the internal kernel method from accept to accept4.

fixup! Fix non-blocking accept4(2) and getting the unix socket peer address.
2017-02-26 22:24:18 +01:00
Jonas 'Sortie' Termansen fcefd86432 Implement shutdown(2). 2017-02-18 15:29:40 +01:00
Jonas 'Sortie' Termansen 7a8a71674e Move readv/writev family and sendmsg/recvmsg into drivers. 2017-02-13 22:04:21 +01:00
Jonas 'Sortie' Termansen db7182ddc3 Add support for sessions.
This change refactors the process group implementation and adds support
for sessions. The setsid(2) and getsid(2) system calls were added.

psctl(2) now has PSCTL_TTYNAME, which lets you get the name of a process's
terminal, and ps(1) now uses it.

The initial terminal is now called /dev/tty1.

/dev/tty is now a factory for the current terminal.

A global lock now protects the process hierarchy which makes it safe to
access other processes. This refactor removes potential vulnerabilities
and increases system robustness.

A number of terminal ioctls have been added.

This is a compatible ABI change.
2016-11-23 22:30:47 +01:00
Jonas 'Sortie' Termansen d529a1e332 Add factory inode support. 2016-11-23 21:46:06 +01:00
Jonas 'Sortie' Termansen c8487ff12b Fix readlinkat(2) return value truncation. 2016-09-28 20:15:36 +02:00
Jonas 'Sortie' Termansen 05282c86d7 Fix fchownat(2) system call ABI on x86.
This system call has five arguments, of which one is a 64-bit uid_t, and
another is a 64-bit gid_t, which means that 7 registers are needed. However,
x86 only has 5 registers available for system calls. Wrap the system call
with a structure like with mmap(2).
2016-03-26 23:28:36 +01:00
Jonas 'Sortie' Termansen 2b72262b4f Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.

All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.

All imported code from other projects is compatible with this license.

All GPL licensed code from other projects had previously been removed.

Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-05 22:21:50 +01:00
Jonas 'Sortie' Termansen ede0571926 Add UTIME_NOW and UTIME_OMIT. 2016-02-24 17:32:05 +01:00
Jonas 'Sortie' Termansen b0bbf1e013 Fix fsm_mountat(2) error handling. 2016-01-27 21:36:04 +01:00
Jonas 'Sortie' Termansen 2e4b15daed Simplify directory reading. 2016-01-26 18:42:54 +01:00
Jonas 'Sortie' Termansen bff1265d62 Add termios(2). 2016-01-25 15:47:40 +01:00
Jonas 'Sortie' Termansen 9d3aecfa07 Work around sync on close bottleneck. 2016-01-08 01:25:10 +01:00
Jonas 'Sortie' Termansen 055f502c02 Fix dup3(2) O_CLOEXEC and O_CLOFORK handling. 2016-01-08 00:36:14 +01:00
Jonas 'Sortie' Termansen 875ce1adf7 Fix faccessat(2) not reporting correct permissions. 2015-10-01 22:50:51 +02:00
Jonas 'Sortie' Termansen 5915e2cd14 Add closefrom(2). 2015-08-26 14:01:25 +02:00
Jonas 'Sortie' Termansen aa09f8cecc Simplify kernel PrepareLookup. 2015-08-22 01:29:38 +02:00
Jonas 'Sortie' Termansen 5589085084 Fix unlinkat potentially following paths twice. 2015-06-27 17:06:32 +02:00
Jonas 'Sortie' Termansen bb70eef85b Replace system call array types with pointers. 2015-01-23 14:52:51 +01:00
Jonas 'Sortie' Termansen d890d3082d Add umount(2) and unmountat(2). 2014-12-26 21:45:29 +01:00
Jonas 'Sortie' Termansen dd950fc996 Add AT_EACCESS support to faccessat(2). 2014-12-03 14:19:58 +01:00
Jonas 'Sortie' Termansen ae4534aae1 Update kernel/descriptor.cpp to current coding conventions. 2014-12-03 14:19:58 +01:00
Jonas 'Sortie' Termansen ba1e0882ec Initialize system call table at compile time. 2014-12-03 14:19:49 +01:00
Jonas 'Sortie' Termansen bb3f591057 Add symbolic links. 2014-12-02 17:36:36 +01:00
Jonas 'Sortie' Termansen 536d7a06f5 Add fcntl(F_PREVFD) and fcntl(F_NEXTFD). 2014-12-01 21:39:05 +01:00
Jonas 'Sortie' Termansen 577f2f700b Add shutdown(2). 2014-11-27 01:39:53 +01:00
Jonas 'Sortie' Termansen a8a8154adc Add getpeername(2) and getsockname(2). 2014-11-27 01:39:11 +01:00
Jonas 'Sortie' Termansen 01402052f6 Add tcgetblob(2) and tcsetblob(2). 2014-11-25 18:40:50 +01:00
Jonas 'Sortie' Termansen a24ecf4b83 Add getsockopt(2) and setsockopt(2). 2014-11-18 18:10:22 +01:00
Jonas 'Sortie' Termansen 708bcb4735 Add recvmsg(2) and sendmsg(2). 2014-11-18 16:27:19 +01:00
Jonas 'Sortie' Termansen 933720e0b0 Remove obsolete system calls. 2014-09-25 17:49:02 +02:00
Jonas 'Sortie' Termansen 874baffd57 Fix wrongly named system call constants. 2014-06-27 15:49:55 +02:00
Jonas 'Sortie' Termansen a935148f60 Add statvfs(2), fstatvfs(2) and fstatvfsat(2). 2014-03-17 19:22:12 +01:00
Jonas 'Sortie' Termansen eaf1618537 Add tcgetwincurpos(2). 2014-03-17 19:22:10 +01:00
Jonas 'Sortie' Termansen 98a87fa1e5 Rename Sortix kernel directory to kernel. 2014-03-01 14:37:39 +01:00
Renamed from sortix/io.cpp (Browse further)