Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp224925pxb; Wed, 20 Oct 2021 20:47:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzOBVolj5GdjqO/DU9b102JLc5eznyozIDm9oPxf8CLpeD05xg9OALigkzCQFbDkevZ+LjD X-Received: by 2002:a63:6dcb:: with SMTP id i194mr2503121pgc.162.1634788020849; Wed, 20 Oct 2021 20:47:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634788020; cv=none; d=google.com; s=arc-20160816; b=d3HAgMGHXOVtrJ+AjaGODOidtZ5oyupl+0hwVi2kM/OIYMwQ1xDxRrY+YlywPEvUTG ox2NjCa9Ldhbnbe6uZyk7+n8UkdQPwen6Qg5MdzNFzukT2LwBVZ3hE/fI1C+Vp1kmbUZ KpUIMQ5ZVg5zj408xFv114CxH3lomVxi+HAvrzKmWspnoL56fJ+bc0SlYvL9ANyYc+bm z47XPQ8yGIUBmwdndXEG5A6XubutWHBnHpTuPH5Nfas7D5RTHPXJTiuCeii40h7U9XNf pmbfyot6QIUgkNZLHcPpMxvhW4xKls+qENAQHHqnGsie/sjZF2ZXiOc7wE4WgpX8Azjj IEvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=mDqWI6DfAAoDrLo3HMtLvXltC73eryXlWAxq3memVRQ=; b=aO+61S/FKkb80TLghn6/rqMUVflX8MtMVhkqh7n2Yz2Qdr5+wvvPATP2dKz+7s22bl yHgJlKytsW+8CcsL4EQV9d36O3FVa5Znj1TMgskyTcRmqRGF30TvwZYWmXC+oQZaG0kH ohIRSs623nBdW0wjEVo0+c7dgZ+kMPmlav3FmguvdEEyHES+qebrtNw9Wfsj6LwPE5Sn cHTtBc+f2OwX0SLSotK1ZIRprj2JKyCRD4i05eyRxK0OOeRBccr/69m7g4hfKwfa6Fbd mgkHjM0YVAgN4o1AIrM0YrCPYo68EbJ+sLnT8IaUgronSEq5UiJ3nYJxpSc7lnfVuBDx c8oQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=oQDNAwXW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a19si1514450pgi.49.2021.10.20.20.46.48; Wed, 20 Oct 2021 20:47:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=oQDNAwXW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231510AbhJUDrv (ORCPT + 99 others); Wed, 20 Oct 2021 23:47:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230434AbhJUDrl (ORCPT ); Wed, 20 Oct 2021 23:47:41 -0400 Received: from mail-pg1-x52d.google.com (mail-pg1-x52d.google.com [IPv6:2607:f8b0:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 537E6C061749; Wed, 20 Oct 2021 20:45:26 -0700 (PDT) Received: by mail-pg1-x52d.google.com with SMTP id e65so21954956pgc.5; Wed, 20 Oct 2021 20:45:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mDqWI6DfAAoDrLo3HMtLvXltC73eryXlWAxq3memVRQ=; b=oQDNAwXWou41LOl80lo8mym0MDCLiuHjXO7v9kWV3cgDj69RcxN4Fqi/YudxpJ4JvP 4pLuXYNMaqjt43GVYo3zEp/KhSW3inmk6rKcfH6RjMgIiAiCV4ACc88Zm7UKGy19fXKr oEJtYq3eUQ4TSR0lEQ96UVlsitDmgApqgujoQi57xKjYI3qEjXO1AOVhhMGgYBvQPyA2 H9Rz/OJ4XIOxXrsUUqKtijGWQRZYt7eBPr04TKafbfBYE/mYK4+9io4dd/tVMwcCMA1t pJlJfvxQEhus0IFpNSu/p3E42PGM+pbOokwU4nf/D8lMJ4mUXlQtMVGzQhDJeTA7H/gV HGFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mDqWI6DfAAoDrLo3HMtLvXltC73eryXlWAxq3memVRQ=; b=ZOMoDemTgGoJJlNpH9HphLa3DOeHkkmglTOo3QU28JRZXVb4KFLC8VRJxUGRrdEnrL EQrb3FGQyzMb+2F7Q9NewkWRVMcWG5BN8jeOKDYhqu+XwugEZqsbW9IEH0XvUbAHcKUi mxZL/y9U7V8mIaEmm0JgY0wXlxGQ4cfdfV3R5drqIcYOhzQ+Xf6nlh1O0YWrxDVnzA8R dxKeoT0yJ1TAa5o6vyHqa4k6GMnN31D2877YfwYBum2i1RSsQfWXsACa1vtWF0YbdeZK h+d/UPEqje6bDGfTf6iHRi1MU921MwtJclSqNNJ7gyJAWACV8JhMIZMA2iyl5RPF2kZI kRuw== X-Gm-Message-State: AOAM531cfImUqcWBQmN2ziiIyXthn0VDpj8Fq9/G9xX8lWA7UsfvP8/X cB0lGEVsmMfKwe53xbA7LWo= X-Received: by 2002:a05:6a00:1686:b0:44d:50e:de9e with SMTP id k6-20020a056a00168600b0044d050ede9emr3012092pfc.4.1634787925875; Wed, 20 Oct 2021 20:45:25 -0700 (PDT) Received: from localhost.localdomain ([140.82.17.67]) by smtp.gmail.com with ESMTPSA id bp19sm3651077pjb.46.2021.10.20.20.45.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Oct 2021 20:45:25 -0700 (PDT) From: Yafang Shao To: keescook@chromium.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, pmladek@suse.com, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, valentin.schneider@arm.com, qiang.zhang@windriver.com, robdclark@chromium.org, christian@brauner.io, dietmar.eggemann@arm.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao Subject: [PATCH v5 02/15] fs/exec: make __get_task_comm always get a nul terminated string Date: Thu, 21 Oct 2021 03:45:09 +0000 Message-Id: <20211021034516.4400-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211021034516.4400-1-laoar.shao@gmail.com> References: <20211021034516.4400-1-laoar.shao@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If the dest buffer size is smaller than sizeof(tsk->comm), the buffer will be without null ternimator, that may cause problem. We can make sure the buffer size not smaller than comm at the callsite to avoid that problem, but there may be callsite that we can't easily change. Using strscpy_pad() instead of strncpy() in __get_task_comm() can make the string always nul ternimated. Suggested-by: Kees Cook Suggested-by: Steven Rostedt Signed-off-by: Yafang Shao Cc: Mathieu Desnoyers Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Kees Cook Cc: Al Viro Cc: Petr Mladek --- fs/exec.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/exec.c b/fs/exec.c index 404156b5b314..bf2a7a91eeea 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1209,7 +1209,8 @@ static int unshare_sighand(struct task_struct *me) char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk) { task_lock(tsk); - strncpy(buf, tsk->comm, buf_size); + /* The copied value is always null terminated */ + strscpy_pad(buf, tsk->comm, buf_size); task_unlock(tsk); return buf; } -- 2.17.1