From nobody Fri Jan 26 21:02:55 2024 X-Original-To: dev-commits-src-all@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 4TM9Bl46Xwz58qmT; Fri, 26 Jan 2024 21:02:55 +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 4TM9Bl1tYmz4Lv5; Fri, 26 Jan 2024 21:02:55 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1706302975; 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=q0IEgbc48o7rDaI9lXOX7tvEZ5J97obWIkmP9kxaM0E=; b=MrfaiHiqh0sJLvd5p54k4dv7Uoze0Xcr8ErkEJaBCBpbQeHQnAjJrOt7OCmyONNdbxqx6w Vg9c5J1aboX+xe8M7VOEXe1wLJPzSNPGCoSvWMHSZ1lh6RZkirHaZQy1pVYLW8YbCfXrCH 527eU4qLqoqVJgEqFPDd+hOkiyvdgcb0LmV7QnzR4lpiCMhgt1G0o9/4rNpB6AQe/ASkSv o+89L9faSefDF/RylBh3S5Z/kys8eSdHaeOdvlqQ3+AxBS7jsLVTzp0xcJSE0dopyFe+Hm 6oUKKni9Z5XcAk2FcZ8Q8ArQ/GQBWlpWdZTos82iSat70kOmJJrKab41c39E8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1706302975; 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=q0IEgbc48o7rDaI9lXOX7tvEZ5J97obWIkmP9kxaM0E=; b=KZdFvz+136t3rGpJ2jYCe9QOKanrbcJzWMJVnduLOmE7Q2wRUsOi/xWUVP4Ja8/0PyY6zq BC8b+S7MN6L7UT2MbioHSgvU7jPBk9EQCS//Nj8V8Pk+FxAPIrWgbsc4g97xNyBnElvdle 4A3xWjOpxRwji71hY5+7Iv7cJr1PwL8VwgCmjS4pHA28fbSjPYVi3TdrhCnD1YYY+upYEa Ie09jVsS5g/nGmHUruaVTmssy1icNEHYmT+AQIFNtLQY/VOv4du5xTQ8LPz1nEPKF0hDmY LPKG+fBd/Pft460nmZxgrAxI3bvXUsa4deI1cofhZOKAHXdpUGCpIZC5ehdS+Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1706302975; a=rsa-sha256; cv=none; b=SOUP8b4yqnzUBx7owFgkPZ0OOY5q7r/LTVj5vEilFqsgu7NgGwe9AN+BbkUDREG/TGPvE7 MPBuRIPH8bDfJjMyaSlCOtPIellbDnedgT9MjXxDmkdCzomRBSBrDg/p3talPKdvgweRPT Ag1cQo51vUMk92hmXuQhBH9CQz1mgtVKuetBp/BLs369GjHTh1BE/0DRtiUkoT9lc5m1YO zL4XoJjOAkQgAxZXCX3uMOSk2FuZ8c0mE9oeERzq2jLz1wQq7fnvAwcHQ+ip+LO9vaggzI n7kOffx1MFlLDr/yxhmx0FeBhV/48J91GHNxtvihgAJbbhuj6ROssBakF9jawQ== 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 4TM9Bl0zGNzx2T; Fri, 26 Jan 2024 21:02:55 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 40QL2tNe083858; Fri, 26 Jan 2024 21:02:55 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 40QL2tJ3083855; Fri, 26 Jan 2024 21:02:55 GMT (envelope-from git) Date: Fri, 26 Jan 2024 21:02:55 GMT Message-Id: <202401262102.40QL2tJ3083855@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Olivier Certner Subject: git: f68a255ff325 - stable/14 - pthread_attr_get_np(): Use malloc(), report ENOMEM, don't tamper on error List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: olce X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: f68a255ff325258a97edfe317ed8272d59e58104 Auto-Submitted: auto-generated The branch stable/14 has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=f68a255ff325258a97edfe317ed8272d59e58104 commit f68a255ff325258a97edfe317ed8272d59e58104 Author: Olivier Certner AuthorDate: 2024-01-04 17:45:52 +0000 Commit: Olivier Certner CommitDate: 2024-01-26 21:01:30 +0000 pthread_attr_get_np(): Use malloc(), report ENOMEM, don't tamper on error Similarly as in the previous commit, using calloc() instead of malloc() is useless here in the regular case since the subsequent call to cpuset_getaffinify() is going to completely fill the allocated memory. However, there is an additional complication. This function tries to allocate memory to hold the cpuset if it previously wasn't, and does so before the thread lock is acquired, which can fail on a bad thread ID. In this case, it is necessary to deallocate the memory allocated in this function so that the attributes object appears unmodified to the caller when an error is returned. Without this, a subsequent call to pthread_attr_getaffinity_np() would expose uninitialized memory (not a security problem per se, since it comes from the same process) instead of returning a full mask as it would before the failing call to pthread_attr_get_np(). So the caller would be able to notice a change in the state of the attributes object even if pthread_attr_get_np() reported failure, which would be quite surprising. A similar problem that could occur on failure of cpuset_setaffinity() has been fixed. Finally, we shall always report memory allocation failure. This already goes for pthread_attr_init(), so, if for nothing else, just be consistent. Reviewed by: emaste, kib Approved by: emaste (mentor) MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D43329 (cherry picked from commit aadb4a1b3fd185d547087f6eafca6ce0b4df3291) Approved by: markj (mentor) --- lib/libthr/thread/thr_attr.c | 45 ++++++++++++++++++++++++----------------- lib/libthr/thread/thr_private.h | 2 -- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/lib/libthr/thread/thr_attr.c b/lib/libthr/thread/thr_attr.c index 561037fa5eb4..3c03225c33cb 100644 --- a/lib/libthr/thread/thr_attr.c +++ b/lib/libthr/thread/thr_attr.c @@ -129,8 +129,9 @@ __weak_reference(_thr_attr_get_np, _pthread_attr_get_np); int _thr_attr_get_np(pthread_t pthread, pthread_attr_t *dstattr) { - struct pthread_attr attr, *dst; + struct pthread_attr *dst; struct pthread *curthread; + cpuset_t *cpuset; size_t kern_size; int error; @@ -138,35 +139,43 @@ _thr_attr_get_np(pthread_t pthread, pthread_attr_t *dstattr) return (EINVAL); kern_size = _get_kern_cpuset_size(); - if (dst->cpuset == NULL) { - dst->cpuset = calloc(1, kern_size); - dst->cpusetsize = kern_size; - } + if ((cpuset = malloc(kern_size)) == NULL) + return (ENOMEM); + } else + cpuset = dst->cpuset; curthread = _get_curthread(); /* Arg 0 is to include dead threads. */ if ((error = _thr_find_thread(curthread, pthread, 0)) != 0) - return (error); - - attr = pthread->attr; - if ((pthread->flags & THR_FLAGS_DETACHED) != 0) - attr.flags |= PTHREAD_DETACHED; + goto free_and_exit; error = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, TID(pthread), - dst->cpusetsize, dst->cpuset); - if (error == -1) + kern_size, cpuset); + if (error == -1) { + THR_THREAD_UNLOCK(curthread, pthread); error = errno; + goto free_and_exit; + } - THR_THREAD_UNLOCK(curthread, pthread); + /* + * From this point on, we can't fail, so we can start modifying 'dst'. + */ - if (error == 0) - memcpy(&dst->pthread_attr_start_copy, - &attr.pthread_attr_start_copy, - offsetof(struct pthread_attr, pthread_attr_end_copy) - - offsetof(struct pthread_attr, pthread_attr_start_copy)); + *dst = pthread->attr; + if ((pthread->flags & THR_FLAGS_DETACHED) != 0) + dst->flags |= PTHREAD_DETACHED; + THR_THREAD_UNLOCK(curthread, pthread); + + dst->cpuset = cpuset; + dst->cpusetsize = kern_size; return (0); + +free_and_exit: + if (dst->cpuset == NULL) + free(cpuset); + return (error); } __weak_reference(_thr_attr_getdetachstate, pthread_attr_getdetachstate); diff --git a/lib/libthr/thread/thr_private.h b/lib/libthr/thread/thr_private.h index 3475029f8996..3fc4d02c611e 100644 --- a/lib/libthr/thread/thr_private.h +++ b/lib/libthr/thread/thr_private.h @@ -261,7 +261,6 @@ struct pthread_atfork { }; struct pthread_attr { -#define pthread_attr_start_copy sched_policy int sched_policy; int sched_inherit; int prio; @@ -271,7 +270,6 @@ struct pthread_attr { void *stackaddr_attr; size_t stacksize_attr; size_t guardsize_attr; -#define pthread_attr_end_copy cpuset cpuset_t *cpuset; size_t cpusetsize; };