From patchwork Tue Oct 29 08:01:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Marchand X-Patchwork-Id: 13854438 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0B3E7200BA5 for ; Tue, 29 Oct 2024 08:01:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730188890; cv=none; b=inXq3PRupri0nri8DKdp3PrX5+PdsYxV1FyYNSoZ9v0R3jvJGXdLyeD8qiZGTOMldNw5ZjcFbZzZcAVuUkff60/ggtf/3VN2zwDB1POXiPzx7KwIOXLxv938ZSeFcUOk2tCm58mvjAlZUL+MfwhTJn4RSoZTZf0HH2+BQ8ytxN4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730188890; c=relaxed/simple; bh=giqtZqw4g3bgGEGBGB08tsN55IgWP62BpYVg6BGefaI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I6axNY24BDzAtraClxz2IQ9yUuCdbRVLehqZwGUW0ZHuFXRHuHd4RXyK5B2FNgReQ3gCIuLkrJUcm4XaUetpD/Gt52LjmSN4y3VTX4PBpqHccBiri1PiWuBnSkrU1GXiIcV914d2I9G9piyUdq/2E1k4bNHCYrEri1+13nyZ2F4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=gyGZbO98; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="gyGZbO98" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730188886; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ey5HutQo4jyas6zbbnfyt7g7vlMu5o7vz3BFdixFL4A=; b=gyGZbO98wBRCzO3f8qCAnirZpPRLymcmIEzQyGKv1eLR062IuH0bAzjBe9lv2ICAqcNpKi 0OiwX6ArfIrREgwMXMjQz52R8nIEAOnfll4swkI4rKA0Nb1K+XH6YH/hiBMDGs3NaA+L+c cIYh3PaG09xlUzwxSJ4qXmQBl2kliJI= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-418-Geg4cK_4OEa2q_dZZjsUzw-1; Tue, 29 Oct 2024 04:01:25 -0400 X-MC-Unique: Geg4cK_4OEa2q_dZZjsUzw-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 923661955D42; Tue, 29 Oct 2024 08:01:24 +0000 (UTC) Received: from fedora (unknown [10.45.224.46]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id 867BA19560A3; Tue, 29 Oct 2024 08:01:22 +0000 (UTC) Received: by fedora (sSMTP sendmail emulation); Tue, 29 Oct 2024 09:01:21 +0100 From: "Jerome Marchand" To: Linux Trace Devel Cc: Steven Rostedt , Jerome Marchand Subject: [PATCH 1/8] trace-cmd lib: Prevent a memory leak in handle_options() Date: Tue, 29 Oct 2024 09:01:10 +0100 Message-ID: <20241029080117.625177-2-jmarchan@redhat.com> In-Reply-To: <20241029080117.625177-1-jmarchan@redhat.com> References: <20240605134054.2626953-1-jmarchan@redhat.com> <20241029080117.625177-1-jmarchan@redhat.com> Precedence: bulk X-Mailing-List: linux-trace-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Buf isn't always fred in the error path. Instead of freing buf at the end of the loop, free it in the exit path and before reallocating it. Fixes a RESOURCE_LEAK error (CWE-772) Signed-off-by: Jerome Marchand --- lib/trace-cmd/trace-input.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index 8b6e3d0c..ad662fc6 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -4006,7 +4006,7 @@ static int handle_options(struct tracecmd_input *handle) char *cpustats = NULL; struct hook_list *hook; bool compress = false; - char *buf; + char *buf = NULL; int cpus; int ret; @@ -4036,6 +4036,7 @@ static int handle_options(struct tracecmd_input *handle) ret = read4(handle, &size); if (ret) goto out; + free(buf); buf = malloc(size); if (!buf) { ret = -ENOMEM; @@ -4189,14 +4190,12 @@ static int handle_options(struct tracecmd_input *handle) tracecmd_warning("unknown option %d", option); break; } - - free(buf); - } ret = 0; out: + free(buf); if (compress) in_uncompress_reset(handle); return ret; From patchwork Tue Oct 29 08:01:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Marchand X-Patchwork-Id: 13854440 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 185DE20370B for ; Tue, 29 Oct 2024 08:01:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730188896; cv=none; b=Ag1yjo+gEzl+QXUyt/o+Fhx2fuMDfWAgYHhSZMbSoitV7De6i7rTmxe2lUgfgDQR3RHgw1Tg3rmY57gQ2tQ1WgKiqotHUbZs3eGixvrR4j/LlRkfwc5CDouMB7jS4HthzHBhvSsGc34ctr5hzd1JU3pJaT/svbbHfH3rIbouLNE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730188896; c=relaxed/simple; bh=6Z7rOhZJRWa5BkDMs9A6UPerFDTmUoJeFZvlbBoHrFE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DMLCMYHx3gQ4ksVxdicBKWnyb7N4esI1Hh4xWCf/sL5W7LB8qe4EJFafR+DTZTWsG2ZscMKRQFF5Yur/POGKh0CtfG5LvAuLb+Swoi3J6bGefDbbFkqJSAGA9lIihG3tJnqgBNJti2OGV2Y/wqwugtHKe/rzodsvYbcvrvLQnBg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=MuKRGwHW; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="MuKRGwHW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730188892; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7Opl3HJFj4EUUlrwA3RES9A46e65pslUfIiInT5P0DE=; b=MuKRGwHWZ1fzvXB+ZmTu7gOL8MlAUAQuMXdUTXK6b9fzmra8RZomKlO2y/hFOwhAWMKKfp bmm084W4uoeLMoLl4661fS7a7riF3U4i9/r+2kOIUTJomO7gBBprNlv+o6TJuFQTbkyBtv pGPcEYwlgpnceMaKcUTzR14tRpwC6Ps= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-536-TMqIKZSDMkumOO5LK39eXQ-1; Tue, 29 Oct 2024 04:01:29 -0400 X-MC-Unique: TMqIKZSDMkumOO5LK39eXQ-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 274FB19560BE; Tue, 29 Oct 2024 08:01:28 +0000 (UTC) Received: from fedora (unknown [10.45.224.46]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id 3535A1956086; Tue, 29 Oct 2024 08:01:25 +0000 (UTC) Received: by fedora (sSMTP sendmail emulation); Tue, 29 Oct 2024 09:01:24 +0100 From: "Jerome Marchand" To: Linux Trace Devel Cc: Steven Rostedt , Jerome Marchand Subject: [PATCH 2/8] trace-cmd record: Prevent a memory leak in show_error() Date: Tue, 29 Oct 2024 09:01:11 +0100 Message-ID: <20241029080117.625177-3-jmarchan@redhat.com> In-Reply-To: <20241029080117.625177-1-jmarchan@redhat.com> References: <20240605134054.2626953-1-jmarchan@redhat.com> <20241029080117.625177-1-jmarchan@redhat.com> Precedence: bulk X-Mailing-List: linux-trace-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 In show_error() the pointer p is used for several functions. At first, it contain a substring of path. Then it is replaced by either an allocated string containing the path to the error log file or the result of read_path(), neither of which are freed when exiting. Free p in both case in the exit path. Fixes a RESOURCE_LEAK error (CWE-772) Signed-off-by: Jerome Marchand --- tracecmd/trace-record.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index a008cdfd..3c42cdf0 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -2374,6 +2374,7 @@ static void show_error(const char *file, const char *type) out: printf("Failed %s of %s\n", type, file); + free(p); free(path); return; } From patchwork Tue Oct 29 08:01:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Marchand X-Patchwork-Id: 13854441 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F3E2A20124E for ; Tue, 29 Oct 2024 08:01:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730188897; cv=none; b=sYQf/ljVmhNo5sVjBTIhcuHfVZWQp7XFjpOt4z1KDSHNZzGD2FzMy+dbvWNv+4NwuFQQWjkoBB41ue7QGU0yfL0iP8eZSfKA5eVzl7qhGzdRMvVQey6pwYScnEAYECTvQOENDIC+XjY5pwh4G6A6LDQHSVnw16u+kJPx9GRcXcM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730188897; c=relaxed/simple; bh=VZxN4LbskOWon1VUFUedEZLQsJHIIgSyH6+8LS1aIZc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ocJbcL5p3gZI1fDzILMedE5+MhGoGTOpwUrIprSTXCcaiHhSFYAFh8o7ITu4JUQxOiMuC7hFs+1PZJwJTZH1nFZNXp/oLrZJ1XW3jDFWIHYsUcdA4zO+nXQ+CS2GsbQgy8G81dA9VvFP+ru9ub8q7sfG7Y2nSnsy1YuO7OrxPso= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Fl3kiret; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Fl3kiret" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730188893; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2WvBiEf0z8FYIvswQybNZaHNzUUCmJuFl+idRkfxYHs=; b=Fl3kiretZb02Izsmd2gdC9y9BqQmfcCDgsZOWEufWeR2Y6ZHCWHvlYu3ZWcOoO9gVXCmOi 7c6i6PUkn2Mb3DW7MkZo9VP20UvZJ4T9qkzI+6lusZS1pqYt5nW31fpMyJ33ex2fzuuPfT ZJxyhjZxi+Zx4MOvZN/GMzsOrt+yDmo= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-269-eb7ryV0OOneeVde3K8FWNA-1; Tue, 29 Oct 2024 04:01:32 -0400 X-MC-Unique: eb7ryV0OOneeVde3K8FWNA-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id AE81019560A5; Tue, 29 Oct 2024 08:01:31 +0000 (UTC) Received: from fedora (unknown [10.45.224.46]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id BD71A195607C; Tue, 29 Oct 2024 08:01:29 +0000 (UTC) Received: by fedora (sSMTP sendmail emulation); Tue, 29 Oct 2024 09:01:28 +0100 From: "Jerome Marchand" To: Linux Trace Devel Cc: Steven Rostedt , Jerome Marchand Subject: [PATCH 3/8] trace-cmd lib: Check the return value of do_lseek() in trace_get_options() Date: Tue, 29 Oct 2024 09:01:12 +0100 Message-ID: <20241029080117.625177-4-jmarchan@redhat.com> In-Reply-To: <20241029080117.625177-1-jmarchan@redhat.com> References: <20240605134054.2626953-1-jmarchan@redhat.com> <20241029080117.625177-1-jmarchan@redhat.com> Precedence: bulk X-Mailing-List: linux-trace-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Check that do_lseek doesn't fail before calling malloc() with a -1 argument. This is flagged as an overrun error (CWE-119) by static anaysis because of the call to read() later, but I don't imagine that malloc would succeed. Signed-off-by: Jerome Marchand --- lib/trace-cmd/trace-output.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index 66e11ddc..8bc9325c 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -2070,6 +2070,8 @@ __hidden void *trace_get_options(struct tracecmd_output *handle, size_t *len) } offset = do_lseek(&out_handle, 0, SEEK_CUR); + if (offset == (off_t)-1) + goto out; buf = malloc(offset); if (!buf) goto out; From patchwork Tue Oct 29 08:01:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Marchand X-Patchwork-Id: 13854442 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8272E2038C9 for ; Tue, 29 Oct 2024 08:01:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730188900; cv=none; b=fvKZuNwplASnR8SSH9XTCF1imaHK2KNx16pXZRNxckE/SRysYXAWSWN1w+M4dRVoDD3IRbqfdrqduOSMWVMTqpz1pbAXHVIOssxW1Vhun8R397O8xTFPZbBFITLLrYK97PDT9PnOEUU9phfrt2al9LTpxLRrTp8BA0/JvoLG/u8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730188900; c=relaxed/simple; bh=F4LKKSqlQBR5xasbPDUuFdc/tLzRaxE9/lfyqsxCeKE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ON5wU1M5IOYR4Rb2KYUIQ3mlickI4FB6HnGN49+vd2YS6ZQkzCZHtMyPWSOJj9yWw9TkE+iCpYRY1fJwKWYlEv8ff5QceMJuj2o10ReiY10FgN4zmWgDIrimHFcIJcGouw3cQh6JnO0O+t0m1SM67GpumIE9i7XQrlPMudCaW/Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=V/ZheVdh; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="V/ZheVdh" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730188897; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=i3r9A8johaFWCNhdmMXpn4m3fhPGaP9hCNGSSFl7TG8=; b=V/ZheVdhlAzG9crMn0Nqs4qHMzmBGgfi5hBBMMhtmWf9W2hLYz2VJtjuWSslhKVKfA1PP1 xWDdRwTnT7fDaRtBzRLnkrKFHIZaqlAM1jX7cuEi08FEHALW/r5XR9tN7UgnhWl/DcFb3X pTyUQ3q6/ALlle3fEaqLuNqvdMLGmfc= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-648-rhLDQSfzMI-Er8UapvRhdA-1; Tue, 29 Oct 2024 04:01:36 -0400 X-MC-Unique: rhLDQSfzMI-Er8UapvRhdA-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 74B9819560B4; Tue, 29 Oct 2024 08:01:35 +0000 (UTC) Received: from fedora (unknown [10.45.224.46]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id 6B31B19560A2; Tue, 29 Oct 2024 08:01:33 +0000 (UTC) Received: by fedora (sSMTP sendmail emulation); Tue, 29 Oct 2024 09:01:32 +0100 From: "Jerome Marchand" To: Linux Trace Devel Cc: Steven Rostedt , Jerome Marchand Subject: [PATCH 4/8] trace-cmd dump: Prevent buffer overrun in dump_clock() Date: Tue, 29 Oct 2024 09:01:13 +0100 Message-ID: <20241029080117.625177-5-jmarchan@redhat.com> In-Reply-To: <20241029080117.625177-1-jmarchan@redhat.com> References: <20240605134054.2626953-1-jmarchan@redhat.com> <20241029080117.625177-1-jmarchan@redhat.com> Precedence: bulk X-Mailing-List: linux-trace-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 The clock isn't big enough to hold the string with the null terminating character. Worse, clock[size], which is out of range, is set to 0. Allocate a big enough buffer. Fixes an OVERRUN error (CWE-119) Signed-off-by: Jerome Marchand --- tracecmd/trace-dump.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tracecmd/trace-dump.c b/tracecmd/trace-dump.c index 11c1baf1..0a21356e 100644 --- a/tracecmd/trace-dump.c +++ b/tracecmd/trace-dump.c @@ -961,7 +961,7 @@ static void dump_clock(int fd) } if (read_file_number(fd, &size, 8)) die("cannot read clock size"); - clock = calloc(1, size); + clock = calloc(1, size + 1); if (!clock) die("cannot allocate clock %lld bytes", size); From patchwork Tue Oct 29 08:01:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Marchand X-Patchwork-Id: 13854443 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8321B200BA5 for ; Tue, 29 Oct 2024 08:01:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730188906; cv=none; b=Rzfl2Gq6FRRxQEKnUKMaeSZAuwZC68rJQzV2pgRa6cgqxvMvpxxy8oKa+Q2yw1SyJ0xdumS+EWVem3GvX8i1VzU0hM7yshd+RT0IYOrfZT4PqlnJ9S/XOCbROt2cab3SdU/mb0DLzAVMdH/N9gGOW8N0bd6OA1Omyz2HmKTuOfo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730188906; c=relaxed/simple; bh=1PWNVvSwBChOcelCpN/62fYpO6yD/aFkjm6W18kH2qU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XSwLD++5zhF10C0G5hIvrXieJRGbOWXB9okClyfSTobCQbmGVH/Wij+G4inRpPaJZTb/8v9+ctXORaZBGmB1z72tJzJ2pFLMtMW7qN11zMTx9mJyCQ4ZZkoIGe8fMsNrfDk+XdqMModPFeYGNJ7ERnh7SIzwFXs7tdjmA6ozrUs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=AIlIwZbs; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="AIlIwZbs" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730188903; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cYwY3B00Kew8YA/PeZK0DP8ZifQytq+XGooNYoSh/ME=; b=AIlIwZbsdGR1cfURO2Q54hcj19A36ghvc6Y6B3pLdTLX2mmwsVOzwl2dmwQo2JLXsFtHIt 1WKpUJrALZBlAu2OmUaAWZ5YjmsY5R8QBVfISrb/elbF0dv56QXENcmZVZ3w29/BjcLTBr LomaLkGn3I78tGOcnSTCliO/CiFdlCY= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-359-AHznrfWgOgyBLrUPFUJjNQ-1; Tue, 29 Oct 2024 04:01:39 -0400 X-MC-Unique: AHznrfWgOgyBLrUPFUJjNQ-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1D70F19560BD; Tue, 29 Oct 2024 08:01:39 +0000 (UTC) Received: from fedora (unknown [10.45.224.46]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id 1236119560A2; Tue, 29 Oct 2024 08:01:36 +0000 (UTC) Received: by fedora (sSMTP sendmail emulation); Tue, 29 Oct 2024 09:01:35 +0100 From: "Jerome Marchand" To: Linux Trace Devel Cc: Steven Rostedt , Jerome Marchand Subject: [PATCH 5/8] trace-cmd record: Prevent memory leak in setup_network() Date: Tue, 29 Oct 2024 09:01:14 +0100 Message-ID: <20241029080117.625177-6-jmarchan@redhat.com> In-Reply-To: <20241029080117.625177-1-jmarchan@redhat.com> References: <20240605134054.2626953-1-jmarchan@redhat.com> <20241029080117.625177-1-jmarchan@redhat.com> Precedence: bulk X-Mailing-List: linux-trace-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Because of the again label, msg_handle can be already allocated if we exit after we got a negative socket file descriptor. Free it there. Also unassign msg_handle->fd as to not double close sfd. Fixes a RESOURCE_LEAK error (CWE-772) Signed-off-by: Jerome Marchand --- tracecmd/trace-record.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index 3c42cdf0..7e84e897 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -3904,6 +3904,7 @@ static struct tracecmd_msg_handle *setup_network(struct buffer_instance *instanc if (sfd < 0) { free(thost); + tracecmd_msg_handle_close(msg_handle); return NULL; } @@ -3934,6 +3935,7 @@ static struct tracecmd_msg_handle *setup_network(struct buffer_instance *instanc if (msg_handle->version == V1_PROTOCOL) { /* reconnect to the server for using the v1 protocol */ close(sfd); + msg_handle->fd = -1; free(host); host = NULL; goto again; From patchwork Tue Oct 29 08:01:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Marchand X-Patchwork-Id: 13854444 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA4B6200BA5 for ; Tue, 29 Oct 2024 08:01:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730188909; cv=none; b=Ot5c7s8H1auS8mZFEMFgBwhnhFdGMu6KO+lNGZWPIRmJcg4L4/4EVHqbHvS516VMQmYckZ9GzYosTz3DMKwgvAgNmsk8IBVf7lnJTsgecAUUwZ2cDBJXUwC3vdlCx8DkWdJQbwGM9vlz914ACPWDGYhEZiCJDwaNjSPntYtDULI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730188909; c=relaxed/simple; bh=dhFHdDLvkouFEhL1umClPXy5V9Enlb+QiNn74C9aaPs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kJVVqPkSLQSYkYVWsANGvEmv/r1a4Af2/J+0SGNR5tiVIcZRujFwbjnDD5J85XzpPAu9tiawl3iQAz9tCfigBTi+rOH/hUUWa0HS+Z9SAiMN21I+HL2922JlTRbomTY/8yxcyxg2eVN7VqNkbZFYgbphhb6GCE21tPr9HOiAvXA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=fnxDNQTE; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="fnxDNQTE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730188906; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jbqA9mMkQdEDRE2KPJYYb+Bc9pX4LLljhqz/9uWcZL0=; b=fnxDNQTEj3PjY+cdqjXgm/bHV965VvAeJKSZZJxdnp5cS4+IAT1bbLNBQKWKb+W8S2q2j3 sUhoU4QiN+wuKFjeTinKj0VafnAUc1/JCP4aFabxouMFOQ3P0YmJ3RORy3csqtsxHpNgyi fRND0jDhp0Ak8wc5CuGA6KzXuU4PCAo= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-599-2tE3BzTfNsyhpvW3sj9B4w-1; Tue, 29 Oct 2024 04:01:43 -0400 X-MC-Unique: 2tE3BzTfNsyhpvW3sj9B4w-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A19481955D44; Tue, 29 Oct 2024 08:01:42 +0000 (UTC) Received: from fedora (unknown [10.45.224.46]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id B010B1956088; Tue, 29 Oct 2024 08:01:40 +0000 (UTC) Received: by fedora (sSMTP sendmail emulation); Tue, 29 Oct 2024 09:01:39 +0100 From: "Jerome Marchand" To: Linux Trace Devel Cc: Steven Rostedt , Jerome Marchand Subject: [PATCH 6/8] trace-cmd lib: Prevent memory leak in tracecmd_create_event_hook() Date: Tue, 29 Oct 2024 09:01:15 +0100 Message-ID: <20241029080117.625177-7-jmarchan@redhat.com> In-Reply-To: <20241029080117.625177-1-jmarchan@redhat.com> References: <20240605134054.2626953-1-jmarchan@redhat.com> <20241029080117.625177-1-jmarchan@redhat.com> Precedence: bulk X-Mailing-List: linux-trace-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Free hook and hook->str in the error path. Fixes a RESOURCE_LEAK error (CWE-772) Signed-off-by: Jerome Marchand --- lib/trace-cmd/trace-hooks.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/trace-cmd/trace-hooks.c b/lib/trace-cmd/trace-hooks.c index a58b5356..aa12f6e9 100644 --- a/lib/trace-cmd/trace-hooks.c +++ b/lib/trace-cmd/trace-hooks.c @@ -151,6 +151,8 @@ struct hook_list *tracecmd_create_event_hook(const char *arg) invalid_tok: tracecmd_warning("Invalid hook format '%s'", arg); + free(hook->str); + free(hook); return NULL; } From patchwork Tue Oct 29 08:01:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Marchand X-Patchwork-Id: 13854445 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 68B632036E3 for ; Tue, 29 Oct 2024 08:01:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730188914; cv=none; b=r5YZdGcxkcP7PgdQbkmdGsbYxoF9uYHUV3MTo+NoEnUUwFVjvxmhADpE5TqDQWavxvL8rJHslS6dq6HcBlYIiWJP4plVbuv82qoutKTGBKf1yApMaTnMUF7ugbfWtB5/dqnnY0+SJlOeKs4uT15vV+Pr+Pbr0fw/su4gf5xl65k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730188914; c=relaxed/simple; bh=wtEbpwkBsdhYVWXR9FaWVmGbU36x9ZObIi8vIn+vcQk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jEsC2iuYthGcILc0+7Xm/nGuCiMZg9xVH4QBthw5SF6TM2g2gBuM+5zX0n1aePtQzk0qSesBFod9e/ll3kDnOS8AW0Xp1eTd/pESuPoHrchYXUaWybCAc5r3GlEZqPRdx3l91G5Z/LuMbXy6lrR17Bv8FAmCBE3QyOqFhemITSM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=A9jhzmip; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="A9jhzmip" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730188911; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jCz59QQqMSxslB6F+LaRs26YFQiOAZUg+f5F0n1+eTk=; b=A9jhzmipW/ayvPcEnFGySwbopy9VlcZRgWb2RznFyIZ9ysUhy9+DNeYzsQoXknZ4s5i0g0 TbAFBmIKnLfhysivf0JenV/3+/AZ9CevA0CXGnScaKQxcNqXfw6xeDqYmo5ivpfTCJncYl wBtXYkCUofhyhkQh7tMgs09CSLfYQaU= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-262-t4p7FqkMOpSy1tuoxZrBQg-1; Tue, 29 Oct 2024 04:01:47 -0400 X-MC-Unique: t4p7FqkMOpSy1tuoxZrBQg-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 321811955EAC; Tue, 29 Oct 2024 08:01:46 +0000 (UTC) Received: from fedora (unknown [10.45.224.46]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id 4255F19560A3; Tue, 29 Oct 2024 08:01:44 +0000 (UTC) Received: by fedora (sSMTP sendmail emulation); Tue, 29 Oct 2024 09:01:42 +0100 From: "Jerome Marchand" To: Linux Trace Devel Cc: Steven Rostedt , Jerome Marchand Subject: [PATCH 7/8] trace-cmd mem: Prevent a memory leak in trace_mem() Date: Tue, 29 Oct 2024 09:01:16 +0100 Message-ID: <20241029080117.625177-8-jmarchan@redhat.com> In-Reply-To: <20241029080117.625177-1-jmarchan@redhat.com> References: <20240605134054.2626953-1-jmarchan@redhat.com> <20241029080117.625177-1-jmarchan@redhat.com> Precedence: bulk X-Mailing-List: linux-trace-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Close the tracecmd handle in the error path. Fixes a RESOURCE_LEAK error (CWE-772) Signed-off-by: Jerome Marchand --- tracecmd/trace-mem.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tracecmd/trace-mem.c b/tracecmd/trace-mem.c index 3e1ac9f3..b8babbbc 100644 --- a/tracecmd/trace-mem.c +++ b/tracecmd/trace-mem.c @@ -554,10 +554,8 @@ void trace_mem(int argc, char **argv) die("can't open %s\n", input_file); ret = tracecmd_read_headers(handle, 0); - if (ret) - return; - - do_trace_mem(handle); + if (!ret) + do_trace_mem(handle); tracecmd_close(handle); } From patchwork Tue Oct 29 08:01:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Marchand X-Patchwork-Id: 13854446 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 08D2A202F9F for ; Tue, 29 Oct 2024 08:01:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730188917; cv=none; b=p3tLTUyEmfdw84XPpsHJDbgVN0OTC9VGZeQm+AkG9NU57KUx4spIzOhblwQYaAvuPOibWPPLLmu/8hljvlJViLYfZ6Cw2ETkFSpkNVLBBoW1FHqizZOznc68VkX2lFzVKYHV91RK3SW9WO/cW9MRlqWEgfRiRZZY0kheCb6oAjY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730188917; c=relaxed/simple; bh=gygKEqXtXURUnCpUPXOXFPkv5nN7Xdk2gmwz8vAFPag=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qetTaTL9ryVJGRz0UInDvBCUZUBDD1RAvCLQWKMLjJWnzO2TksoWXNKlXtNwACf1Tea4H26EQJtFUwa+SWpEQyEDnNqqj7LYLzUdxYDwNroYnQqSzwcKdIccvr2epRmTOLBlCPXvU3ZZXMBU5MLLPQcoyjqF9Vqwhlc2IllKXzw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Nmw7u9Hn; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Nmw7u9Hn" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730188914; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KPQqvL0DfWtqWZ6us6Jg3DJiuib3ufD5gEfVNXRE4Bk=; b=Nmw7u9HnayNErLbojSBSfSia7Zojwk5JJXXrI1kZHS/Cm8VPV2q5tqzTMPu/VEKqjvsHbz o6gGE35nWwqBAFaXVCa8zvQawMpVQ2f6rmp6o1gBSEK8kDrb95rNBVphx/hJOTPzSFYLek PU+RqcDzQa0QiPBtlXGz4ZAcxNtNE1E= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-493-DH29Edq2PUaXBmXkCTS8yA-1; Tue, 29 Oct 2024 04:01:50 -0400 X-MC-Unique: DH29Edq2PUaXBmXkCTS8yA-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9BF981955BCF; Tue, 29 Oct 2024 08:01:49 +0000 (UTC) Received: from fedora (unknown [10.45.224.46]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id A87E730001A7; Tue, 29 Oct 2024 08:01:47 +0000 (UTC) Received: by fedora (sSMTP sendmail emulation); Tue, 29 Oct 2024 09:01:46 +0100 From: "Jerome Marchand" To: Linux Trace Devel Cc: Steven Rostedt , Jerome Marchand Subject: [PATCH 8/8] trace-cmd record: Check the length of the protocol version received Date: Tue, 29 Oct 2024 09:01:17 +0100 Message-ID: <20241029080117.625177-9-jmarchan@redhat.com> In-Reply-To: <20241029080117.625177-1-jmarchan@redhat.com> References: <20240605134054.2626953-1-jmarchan@redhat.com> <20241029080117.625177-1-jmarchan@redhat.com> Precedence: bulk X-Mailing-List: linux-trace-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 In check_protocol_version we compare the protocol version string with the expected one ("V3") with memcmp(). The received string could be longer than the constant string used for the comparison. That could lead to out of range access. Use the known length of the fixed "V3" string for the comparison and check that the received protocol version is not too short. Fixes a OVERRUN error (CWE-119) Signed-off-by: Jerome Marchand --- tracecmd/trace-record.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index 7e84e897..6e9b4535 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -3811,7 +3811,7 @@ static void check_protocol_version(struct tracecmd_msg_handle *msg_handle) msg_handle->version = V1_PROTOCOL; tracecmd_plog("Use the v1 protocol\n"); } else { - if (memcmp(buf, "V3", n) != 0) + if (n < 3 || memcmp(buf, "V3", 3) != 0) die("Cannot handle the protocol %s", buf); /* OK, let's use v3 protocol */ write(fd, V3_MAGIC, sizeof(V3_MAGIC));