Commit Graph

312 Commits

Author SHA1 Message Date
pgoyette e512fb100a There's no COMPAT_60 code left here, so no need for conditional
inclusion of header file.
2023-12-07 09:00:32 +00:00
riastradh a7a3e2ad8d tty(9): Make ttwrite update uio with only how much it has consumed.
As is, it leaves uio in an inconsistent state.  Good enough for the
write(2) return value to be correct for a userland caller to restart
write(2) where it left off, but not good enough for a loop in the
kernel to reuse the same uio.

Reported-by: syzbot+e0f56178d0add0d8be20@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=6290eb02b8fe73361dc15c7bc44e1208601e6af8

Reported-by: syzbot+7caa189e8fccd926357e@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=c0a3b77b4831dfa81fc855857bde81755d246bd3

Reported-by: syzbot+4a1eff91eb4e7c1970b6@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=10523a633a4ad9749f57dc7cf03f9447d518c5b8

Reported-by: syzbot+1d3c280f59099dc82e17@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=8e02ebb0da76a8e286461f33502117a1d30275c6

Reported-by: syzbot+080d51214d0634472b12@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=1f617747db8087e5554d3df1b79a545dee26a650

Reported-by: syzbot+dd50b448e49e5020131a@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=f71c8cef4110b7eeac6eca67b6a4d1f4a8b3e96f

Reported-by: syzbot+26b675ecf0cc9dfd8586@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=57b1901f5b3e090a964d08dd0d729f9909f203be

Reported-by: syzbot+87f0df2c9056313a5c4b@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=67994a3da32d075144e25d1ac314be1d9694ae6e

Reported-by: syzbot+e5bc98e18aa42f0cb25d@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=6374bd286532423c63f2b331748280729134224c

Reported-by: syzbot+7e587f4c5aaaf80e84b3@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=976210ed438d48ac275d77d7ebf4a086e43b5fcb
2023-05-22 14:07:37 +00:00
riastradh a87a5383a9 ttycheckoutq(9): wait=0 always, parameter no longer useful, nix it.
XXX kernel revbump
2023-04-12 06:35:26 +00:00
riastradh b72c60c2e8 ttwrite(9): Assert we mangle uio_resid only if we also return error. 2023-04-11 10:23:47 +00:00
riastradh babfa29c54 ttycheckoutq(9): wait is always 0. Assert it; prune dead branches.
There appear to have been no callers with wait=1 since NetBSD 1.0
from a cursory search.  Let's nix the parameter altogether on the
next kernel revbump.  This logic is probably broken anyway in the
presence of ttycancel, which is necessary for, e.g., yanking USB
serial adapters.
2023-02-17 23:13:01 +00:00
riastradh 2ef45a3b45 tty(9): New ttylock, ttyunlock, ttylocked functions.
These are wrappers around the global tty_lock for now (and the
continued existence of the tty_lock variable is why the ttylock
function has no underscore in its name).  They will assist in
converting drivers to per-tty locking later on.
2022-10-26 23:41:49 +00:00
riastradh 03cc61dfce constty(4): Make MP-safe, take three.
Access to the global constty variable is coordinated as follows:

1. Setting constty to nonnull, with atomic_store_release, is allowed
   only under the new adaptive constty_lock in thread context.  This
   serializes TIOCCONS operations and ensures unlocked readers can
   safely use a constty pointer read with atomic_load_consume.

2. Changing constty from nonnull to null, with atomic_cas_ptr, is
   allowed in any context -- printf(9) uses this to disable a broken
   constty.

3. Reading constty under constty_lock is allowed with
   atomic_load_relaxed, because while constty_lock is held, it can
   only be made null by some other thread/CPU, never made nonnull.

4. Reading constty outside constty_lock is allowed with
   atomic_load_consume in a pserialize read section -- constty is
   only ever made nonnull with atomic_store_release, in (1).
   ttyclose will wait for all these pserialize read sections to
   complete before flushing the tty.

5. To continue to use a struct tty pointer in (4) after the
   pserialize read section has completed, caller must use tty_acquire
   during the pserialize read section and then tty_release when done.
   ttyclose will wait for all these references to drain before
   returning.

These access rules allow us to serialize TIOCCONS, and safely destroy
ttys, without putting any locks on the access paths like printf(9)
that use constty.  Once we set D_MPSAFE, operations on /dev/console
will contend only with other users of the same tty as constty, which
will be an improvement over contending with all other kernel lock
users in the system.

Changes second time around:
- Fix initialization of ok in cons.c cn_redirect.
- Fix reversed sense of conditional in subr_prf.c putone.

Changes third time around:
- Initialize ttyref_cv so we don't panic when trying to use it,
  leading to infinite loop when panic tries to take tty_lock to print
  the panic message while we already hold tty_lock.
2022-10-25 23:21:13 +00:00
riastradh 35c7b9ba47 Revert "constty(4): Make MP-safe."
Something is still busted and this is interfering with the releng
amd64 testbed.
2022-10-07 18:59:37 +00:00
riastradh 05f78a23d4 constty(4): Make MP-safe.
Access to the global constty variable is coordinated as follows:

1. Setting constty to nonnull, with atomic_store_release, is allowed
   only under the new adaptive constty_lock in thread context.  This
   serializes TIOCCONS operations and ensures unlocked readers can
   safely use a constty pointer read with atomic_load_consume.

2. Changing constty from nonnull to null, with atomic_cas_ptr, is
   allowed in any context -- printf(9) uses this to disable a broken
   constty.

3. Reading constty under constty_lock is allowed with
   atomic_load_relaxed, because while constty_lock is held, it can
   only be made null by some other thread/CPU, never made nonnull.

4. Reading constty outside constty_lock is allowed with
   atomic_load_consume in a pserialize read section -- constty is
   only ever made nonnull with atomic_store_release, in (1).
   ttyclose will wait for all these pserialize read sections to
   complete before flushing the tty.

5. To continue to use a struct tty pointer in (4) after the
   pserialize read section has completed, caller must use tty_acquire
   during the pserialize read section and then tty_release when done.
   ttyclose will wait for all these references to drain before
   returning.

These access rules allow us to serialize TIOCCONS, and safely destroy
ttys, without putting any locks on the access paths like printf(9)
that use constty.  Once we set D_MPSAFE, operations on /dev/console
will contend only with other users of the same tty as constty, which
will be an improvement over contending with all other kernel lock
users in the system.

Changes second time around:
- Fix initialization of ok in cons.c cn_redirect.
- Fix reversed sense of conditional in subr_prf.c putone.
2022-10-06 19:58:41 +00:00
riastradh 8c6dbca696 Revert "constty(4): Make MP-safe."
Something appears to be wrong with this.
2022-10-04 05:20:01 +00:00
riastradh 2c7df491bf constty(4): Make MP-safe.
Access to the global constty variable is coordinated as follows:

1. Setting constty to nonnull, with atomic_store_release, is allowed
   only under the new adaptive constty_lock in thread context.  This
   serializes TIOCCONS operations and ensures unlocked readers can
   safely use a constty pointer read with atomic_load_consume.

2. Changing constty from nonnull to null, with atomic_cas_ptr, is
   allowed in any context -- printf(9) uses this to disable a broken
   constty.

3. Reading constty under constty_lock is allowed with
   atomic_load_relaxed, because while constty_lock is held, it can
   only be made null by some other thread/CPU, never made nonnull.

4. Reading constty outside constty_lock is allowed with
   atomic_load_consume in a pserialize read section -- constty is
   only ever made nonnull with atomic_store_release, in (1).
   ttyclose will wait for all these pserialize read sections to
   complete before flushing the tty.

5. To continue to use a struct tty pointer in (4) after the
   pserialize read section has completed, caller must use tty_acquire
   during the pserialize read section and then tty_release when done.
   ttyclose will wait for all these references to drain before
   returning.

These access rules allow us to serialize TIOCCONS, and safely destroy
ttys, without putting any locks on the access paths like printf(9)
that use constty.  Once we set D_MPSAFE, operations on /dev/console
will contend only with other users of the same tty as constty, which
will be an improvement over contending with all other kernel lock
users in the system.
2022-10-03 19:57:05 +00:00
riastradh 4379cfa731 tty(9): New function tty_unit for struct cdevsw::d_devtounit. 2022-04-07 21:46:51 +00:00
riastradh 9b9d720dd6 tty(9): New ttycancel function.
This causes any current and future ttyopens to fail until ttyclose.

This is necessary for revoke to work reliably for device detach like
ucom(4) removable USB devices.  A tty driver for a removable device
needs some way to interrupt a pending .d_open so it returns promptly.
But ttyclose only interrupts ttyopen if it's already sleeping; it
won't cause a concurrent .d_open call which _will call_ but _hasn't
yet called_ ttyopen to avoid sleeping.  Using ttycancel in the tty
driver's .d_cancel makes this work.
2022-03-28 12:39:28 +00:00
msaitoh 8194593f85 s/runable/runnable/ 2021-12-05 07:35:17 +00:00
thorpej 3f8867c66a ttyread_filtops, ttywrite_filtops, ptcread_filtops, and ptcwrite_filtops
are MPSAFE.
2021-09-29 13:14:39 +00:00
thorpej ec9c6f37a8 Consistently reference kn->kn_data only within the lock perimeter in
the filtops f_event() callback.
2021-09-27 00:40:49 +00:00
thorpej 12ae65d98c Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89
2021-09-26 01:16:07 +00:00
thorpej 2ef9bcafb7 Use sel{record,remove}_knote(). 2020-12-11 03:00:09 +00:00
christos ce9395ac74 remove extra break 2020-10-10 18:53:56 +00:00
christos 4963d964ed remove broken copy of TIOCGSID. 2020-10-10 17:25:11 +00:00
christos 0a6d6ce0fd TIOCGSID is used by tcgetsid() so it is not really compat :-)
This should reduce loading the compat module.
2020-10-10 15:59:41 +00:00
nia bf7b939a1a tty: Negating INT_MIN will overflow int, bail out with EINVAL
Detected by UBSan

Reported-by: syzbot+92c0fca82b74a9798b78@syzkaller.appspotmail.com
2020-10-10 14:07:18 +00:00
nia ad11505eeb tty: Avoid undefined behaviour (left shift of 1 by 31 places overflows int)
The valid sizes of the tty input and output queues (according to the man page)
are between 1024 and 65536 and input values are converted to a power of two.

The check on the validity of the range is done after the input values are
converted, however, which means that a hostile program can attempt to set
the queue size to a negative value, and cause integer overflow before
the range is validated.

Detected by UBSan

Reported-by: syzbot+521b73969fd233c49e58@syzkaller.appspotmail.com
2020-10-09 09:03:55 +00:00
maxv 699b2c0a0a Add a check to prevent shift by -1. Not really important in this case,
but to appease KUBSAN.

Reported-by: syzbot+4026e8201b6b484b8cb4@syzkaller.appspotmail.com
2020-08-26 16:36:32 +00:00
maxv 6b1289230e Don't leak an unused sysctl log. Found by kLSan. 2020-06-22 16:29:24 +00:00
ad 0eaaa024ea Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.
2020-05-23 23:42:41 +00:00
christos 81e23aba9b Don't crash if we are on a hippie trail, head full of zombie 2020-01-21 15:25:38 +00:00
skrll 818dfa5d0a Appease gcc 2020-01-07 08:52:47 +00:00
ad e103824f66 ttygetinfo(): avoid crash with zombies. From skrll@, tweaked by me. 2020-01-06 11:18:51 +00:00
skrll ac86769291 KNF 2020-01-02 16:52:55 +00:00
skrll 3e149c20a6 Trailing whitespace 2020-01-02 16:51:54 +00:00
pgoyette 8c2f80f160 Rename the MODULE_*_HOOK() macros to MODULE_HOOK_*() as briefly
discussed on irc.

NFCI intended.

Ride the earlier kernel bump - it;s getting crowded.
2019-03-01 11:06:55 +00:00
pgoyette c1283e70fa Normalize all the compat hooks' names to the form
<subsystem>_<function>_<version>_hook

NFCI

XXX Note that although this introduces a change in the kernel-to-
XXX module interface, we are NOT bumping the kernel version number.
XXX We will bump the version number once the interface stabilizes.
2019-01-29 09:28:50 +00:00
christos c3850d5f86 - provide a hook for the 43 tty ioctls
- make the 60 tty ioctl hook look the same
- fix the tty code to call both hooks and remove unused lock
2019-01-28 15:46:49 +00:00
pgoyette d91f98a871 Merge the [pgoyette-compat] branch 2019-01-27 02:08:33 +00:00
riastradh d1579b2d70 Rename min/max -> uimin/uimax for better honesty.
These functions are defined on unsigned int.  The generic name
min/max should not silently truncate to 32 bits on 64-bit systems.
This is purely a name change -- no functional change intended.

HOWEVER!  Some subsystems have

	#define min(a, b)	((a) < (b) ? (a) : (b))
	#define max(a, b)	((a) > (b) ? (a) : (b))

even though our standard name for that is MIN/MAX.  Although these
may invite multiple evaluation bugs, these do _not_ cause integer
truncation.

To avoid `fixing' these cases, I first changed the name in libkern,
and then compile-tested every file where min/max occurred in order to
confirm that it failed -- and thus confirm that nothing shadowed
min/max -- before changing it.

I have left a handful of bootloaders that are too annoying to
compile-test, and some dead code:

cobalt ews4800mips hp300 hppa ia64 luna68k vax
acorn32/if_ie.c (not included in any kernels)
macppc/if_gm.c (superseded by gem(4))

It should be easy to fix the fallout once identified -- this way of
doing things fails safe, and the goal here, after all, is to _avoid_
silent integer truncations, not introduce them.

Maybe one day we can reintroduce min/max as type-generic things that
never silently truncate.  But we should avoid doing that for a while,
so that existing code has a chance to be detected by the compiler for
conversion to uimin/uimax without changing the semantics until we can
properly audit it all.  (Who knows, maybe in some cases integer
truncation is actually intended!)
2018-09-03 16:29:22 +00:00
maya 4617248544 correct typo: and and -> and
from chris28.
2018-03-30 22:59:43 +00:00
maya 18b796d442 Use C99 initializer for filterops
Mostly done with spatch with touchups for indentation

@@
expression a;
identifier b,c,d;
identifier p;
@@
const struct filterops p =
- 	{ a, b, c, d
+ 	{
+ 	.f_isfd = a,
+ 	.f_attach = b,
+ 	.f_detach = c,
+ 	.f_event = d,
};
2017-10-25 08:12:37 +00:00
christos c0e5049c21 Require exact credential match; this way even if we su to the original user
that created the session, we won't match his credentials.
2016-10-01 04:42:54 +00:00
christos 4b39133eee Weaken the test a bit to still allow non-root to use TIOCSTI; we need to have
the same creds as the session leader process for the tty session.
2016-10-01 03:46:00 +00:00
christos f08a5ec0bf Only allow root to use TIOCSTI. Don't eat the kauth error number.
It is unexpected for an unprivileged process to gain privs by
typing to root's tty:

$ cat installer
#!/bin/sh
whoami
/usr/sbin/sti /dev/tty whoami\\n

$ su unprivileged -c ./installer
unprivileged
$ whoami
root
2016-09-29 21:46:32 +00:00
msaitoh 8bc54e5be6 KNF. Remove extra spaces. No functional change. 2016-07-07 06:55:38 +00:00
christos c4922ba137 Add console-related ioctls. 2015-10-22 17:36:33 +00:00
christos 054ca2c181 add the pty ioctls to pass through. 2015-10-18 15:58:23 +00:00
christos 8c41569f24 handle the hardware layer tty ioctls directly so that we don't need to load
the compat module for normal operations.
2015-10-18 15:14:50 +00:00
gson 42b8d69040 In ttywait_timo(), break out of loop on all errors, not just
EWOULDBLOCK, as ttywait() did prior to 1.265.
2015-08-25 12:55:30 +00:00
pooka d8e04c9094 to garnish, dust with _KERNEL_OPT 2015-08-24 22:50:32 +00:00
gson 2a8f000c61 When closing a tty, limit the amount of time spent waiting for the
output to drain to five seconds so that exiting processes with
buffered output for a serial port blocked by flow control or a pty
that is not being read do not hang indefinitely.  Should fix PRs
kern/12534 and kern/17171.  This is an updated version of the change
of tty.c 1.263.
2015-08-19 12:02:55 +00:00
gson 873b1ca5ad Revert previous; it broke the lib/libc/ttyio/t_ttyio/ioctl test case. 2015-06-14 16:19:31 +00:00
gson 6b1c582456 When closing a tty, limit the amount of time spent waiting for the
output to drain to five seconds so that exiting processes with
buffered output for a serial port blocked by flow control do not
hang indefinitely.  Should fix PR kern/12534.  OK christos.
2015-06-12 17:28:53 +00:00