From nobody Mon Apr 25 07:23:46 2022 X-Original-To: dev-commits-src-main@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 DDFE71A90EFE; Mon, 25 Apr 2022 07:23:46 +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 4KmxMQ3Tmgz3Fms; Mon, 25 Apr 2022 07:23:46 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1650871426; 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=VUvZAEHi1W5iWN8Qtb25kyIH5+5igt3ZFLYX6HFeBmw=; b=nr3PZHs1A6/hdeuSADMSd2N63lqZOxg3NLEPC6IJxL0WPfKoi8cAPfC9000FGWqWLmQgr9 pnybfPvA9e3btNEEWz5QuL3ImZ0tV8X5hwmCFTv96e9d9tehvtTgaWspbxmC6ssGwc7DU0 ngz0WOXgI8RGuNKm6Q7ezRfcjM+N7Qil9Bzu9m4hydkX+mmtUivK+1GuVo1u3/cnlMXeW1 ZpPh5K2FLhW1+t35SX01ezoTYgYsuDcVAnBw29Wwaeonv+mWNw5KA4DwK5DNjKfm+HbZVi m+F4P5mlQplti0UpkzyU2X/qiMfRq8gQSQ3v+gEMNxKzYtILWGLHKARVqTkZZQ== 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 32A5416756; Mon, 25 Apr 2022 07:23:46 +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 23P7NkUR065780; Mon, 25 Apr 2022 07:23:46 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 23P7NkTl065779; Mon, 25 Apr 2022 07:23:46 GMT (envelope-from git) Date: Mon, 25 Apr 2022 07:23:46 GMT Message-Id: <202204250723.23P7NkTl065779@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dmitry Chagin Subject: git: 4a700f3c329a - main - sigtimedwait: Prevent timeout math overflows. List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: 4a700f3c329a48f3a20fd576900820e293c8cad7 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1650871426; 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=VUvZAEHi1W5iWN8Qtb25kyIH5+5igt3ZFLYX6HFeBmw=; b=OKEoNksU/21odzzWJVy4gXBHVZ/LBizrt0DHC1DDmq1eoCWKEjQNnXCl3PSMuSkgfubrU/ dUvuG/URpyuLqgkjLU6nm5yEP+zi99pjRReakT86v3scOce66S5mECkvlx3E9YLwOR7ayk TzlgfH06ncwI/rgIl5IPAALKAbLgryZZhbb6WmtgrNv7ESf4TlRpEWR6FG0/03T4uCiYEI CAEypNqFFk5QOUFctnzuapXBtk5e7RAEQ/EJPn29EedFptP2xouHWpbqDDngVdgBbjOzBa dJJ3bQbKSvhh7X3ndJDBgHhvAoOH4XuIqjCGt+D2EbNrRiz7/r0ihfvzTIlngw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1650871426; a=rsa-sha256; cv=none; b=Cjoc6ZBJEBKgT6fvORxzW1a68EAmS5+D587iP/N1qYRQwEYjVOP13t7z/21mvymT/xQu79 xnJiJJ/8eIritliu5WfytMSEgexR5XQnLjxODEDmO28P695mK5wWNDyw7EkzyAHjDLX7Cc 6T/pbm4GoakIMSlrrt9LW0ya4a6H47h0uwWZUWNtvyaxefx7feWAnsaX/XBzUwxKkbCrns UD1FYwko81sywRTqyq6g8N6V243v3sfkG6DWzaNFUb+TjPx6qPntoiOvmlLeTYgHyr+TXv tFoprfW/jPbzpdJqWygB6K4FC+qTFUjSOt3jixF6SzvriDagDtw2aDsLjm/6yQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=4a700f3c329a48f3a20fd576900820e293c8cad7 commit 4a700f3c329a48f3a20fd576900820e293c8cad7 Author: Dmitry Chagin AuthorDate: 2022-04-25 07:23:15 +0000 Commit: Dmitry Chagin CommitDate: 2022-04-25 07:23:15 +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 --- 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 55b2957d163b..ce6f517262a4 100644 --- a/sys/kern/kern_sig.c +++ b/sys/kern/kern_sig.c @@ -1258,15 +1258,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. */ @@ -1275,10 +1273,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); @@ -1312,21 +1319,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) { @@ -1334,17 +1329,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