From nobody Fri Jun 17 19:39:17 2022 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id ECBE285BFB6; Fri, 17 Jun 2022 19:39:18 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4LPq9d6Xblz3Qtw; Fri, 17 Jun 2022 19:39:17 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1655494758; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=omQ/CSJt+e6IfE2rnOgBZW1ICxN7ZXnXARkdQksObHQ=; b=EHC/SSjUoQ/UpTH6Uxo3HfScVDG46sKAG7/WiDssDfhGGPtSiJw6otBFN7LNCUQm181Orv Tgmdyjk/E0Dg8tqR9iio5E+BLlVXc35kngTHSTDWJ1u15a/W3FAtJE3CjVaMVR60A1xMgG roGU7h4rYnXDlYp4mObm2tVqYrd0MabsN8niun+fJgYdn6ZkbSb1UEVr2VObh6gJrlj8jj ecE7+iqElvtR54XI4BHyLZZzCS/ActGc4HlL/xrTqB01TUTNQ+D0oTn4PNvYH+EgJ1vJCb n5g8qelVMGxLV1YYgKN6+kQBN4TAMBjz687+iJBGboFCVX7Wlcy7LmRaBP5Aag== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 7208A25C51; Fri, 17 Jun 2022 19:39:17 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 25HJdHRu015811; Fri, 17 Jun 2022 19:39:17 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 25HJdH4l015810; Fri, 17 Jun 2022 19:39:17 GMT (envelope-from git) Date: Fri, 17 Jun 2022 19:39:17 GMT Message-Id: <202206171939.25HJdH4l015810@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dmitry Chagin Subject: git: 3130fd2daa5b - stable/13 - sigtimedwait: Prevent timeout math overflows. List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dchagin X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 3130fd2daa5be90eeeedcc516d27883300ec8495 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1655494758; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=omQ/CSJt+e6IfE2rnOgBZW1ICxN7ZXnXARkdQksObHQ=; b=eYqz7MGtMB0gtXJFTDbb6ZSeetWpLkO+AbiqWPv4R8kWpvlQfi9Kl7Lra91pkBvHa50Ad5 KcbjeQNjzRbn5TikC9ZuPCPCsehLnhKNtDfQJM9UaRv1+9/OleaM0ExtsxidncUIv/r+cW SPD96m7WPlFEdDK0MWttGhuxfOHXUNCOA3iVT9ikbWPJX7JlNrzuvshE9eao8p+qyx1lmD aa1VokMxNVXpxyBPUn7OkMNU/UO/Y+OyqeWlw0/yughBatfZotG+clnRdbIchP/XGxl11N GKAYT5bzsUguWLR0CS9ArEnxHGLV53qNfJlRn6Glsi19juFHNqhE5GJKOI09Tw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1655494758; a=rsa-sha256; cv=none; b=vks6X0e3BVBrceQB5Pxcb2frQNgx+B7M265Hjcw5o1rh803p2xZrHtNJL0UKloCF+FHGbg NzLn8Cx58i8IdFjAI82p7uRuprht7FRPwDou+6Ts2qErHvsbLIJ5EIbnt0nHpvce8/PfeW VTdhMlt8dNk6GMRYJDdjR8GYzG9VuBP0MBsjx8pTdW8T3amoLv0mxE5sQoisNxvlaZgCIZ f1l6E5QkMZNVmQokW1MDEDsb5n2teZpms4Zoa2mZPkLtTvOir1bvkPvmgRD2nbNRE9vznu Bk2exIK1nDIvSkXdnbPAk5ld9C8BcffkBWBtnwIcyGvqS0lKj55FNH/6TEtEBw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=3130fd2daa5be90eeeedcc516d27883300ec8495 commit 3130fd2daa5be90eeeedcc516d27883300ec8495 Author: Dmitry Chagin AuthorDate: 2022-04-25 07:23:15 +0000 Commit: Dmitry Chagin CommitDate: 2022-06-17 19:33:55 +0000 sigtimedwait: Prevent timeout math overflows. Our kern_sigtimedwait() calculates absolute sleep timo value as 'uptime+timeout'. So, when the user specifies a big timeout value (LONG_MAX), the calculated timo can be less the the current uptime value. In that case kern_sigtimedwait() returns EAGAIN instead of EINTR, if unblocked signal was caught. While here switch to a high-precision sleep method. Reviewed by: mav, kib In collaboration with: mav Differential revision: https://reviews.freebsd.org/D34981 MFC after: 2 weeks (cherry picked from commit 4a700f3c329a48f3a20fd576900820e293c8cad7) --- sys/kern/kern_sig.c | 49 ++++++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/sys/kern/kern_sig.c b/sys/kern/kern_sig.c index e3d14f8c0141..05712f55195a 100644 --- a/sys/kern/kern_sig.c +++ b/sys/kern/kern_sig.c @@ -1277,15 +1277,13 @@ kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi, struct sigacts *ps; sigset_t saved_mask, new_block; struct proc *p; - int error, sig, timo, timevalid = 0; - struct timespec rts, ets, ts; - struct timeval tv; + int error, sig, timevalid = 0; + sbintime_t sbt, precision, tsbt; + struct timespec ts; bool traced; p = td->td_proc; error = 0; - ets.tv_sec = 0; - ets.tv_nsec = 0; traced = false; /* Ensure the sigfastblock value is up to date. */ @@ -1294,10 +1292,19 @@ kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi, if (timeout != NULL) { if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) { timevalid = 1; - getnanouptime(&rts); - timespecadd(&rts, timeout, &ets); + ts = *timeout; + if (ts.tv_sec < INT32_MAX / 2) { + tsbt = tstosbt(ts); + precision = tsbt; + precision >>= tc_precexp; + if (TIMESEL(&sbt, tsbt)) + sbt += tc_tick_sbt; + sbt += tsbt; + } else + precision = sbt = 0; } - } + } else + precision = sbt = 0; ksiginfo_init(ksi); /* Some signals can not be waited for. */ SIG_CANTMASK(waitset); @@ -1331,21 +1338,9 @@ kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi, * POSIX says this must be checked after looking for pending * signals. */ - if (timeout != NULL) { - if (!timevalid) { - error = EINVAL; - break; - } - getnanouptime(&rts); - if (timespeccmp(&rts, &ets, >=)) { - error = EAGAIN; - break; - } - timespecsub(&ets, &rts, &ts); - TIMESPEC_TO_TIMEVAL(&tv, &ts); - timo = tvtohz(&tv); - } else { - timo = 0; + if (timeout != NULL && !timevalid) { + error = EINVAL; + break; } if (traced) { @@ -1353,17 +1348,13 @@ kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi, break; } - error = msleep(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH, - "sigwait", timo); + error = msleep_sbt(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH, + "sigwait", sbt, precision, C_ABSOLUTE); /* The syscalls can not be restarted. */ if (error == ERESTART) error = EINTR; - /* We will calculate timeout by ourself. */ - if (timeout != NULL && error == EAGAIN) - error = 0; - /* * If PTRACE_SCE or PTRACE_SCX were set after * userspace entered the syscall, return spurious