From patchwork Thu Dec 22 16:42:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Dobriyan X-Patchwork-Id: 35878 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp52885wrn; Thu, 22 Dec 2022 08:45:43 -0800 (PST) X-Google-Smtp-Source: AMrXdXsDjZQ+PcolYhnmnQ+28kChMp0cXn0HwPhYBrAMBCeYIVVSk2UCP26KZDrUQdHlU72IWDRA X-Received: by 2002:a50:ed17:0:b0:46d:6f14:aec with SMTP id j23-20020a50ed17000000b0046d6f140aecmr5463588eds.0.1671727543247; Thu, 22 Dec 2022 08:45:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671727543; cv=none; d=google.com; s=arc-20160816; b=V6mtGdDm5hh/oF2pdopqBxz4yPUajLGtLZyzFBcCyH8QqDPAEYS7k4SSpsAvuFZlRq SmMvpAjZZEBgZk26Vv5BUThekwWdgAJRWbjG7b0sQUbGC2wNvmiDJLCDtZkiq8zJH1CX 9kN7Uym9zvuMQH61age6ThHe1+SPT2LLoMl3MUFD2S0gL4CxIRE7eihhCJwwkD1vCnpU GCWvgGvWW8SNlOFxFAvEvdV/gWmNc67Mb9/kALHlTC+iAz46yRDKWFBKF+1tdXmiUHcZ TaX7H1f6QfADXtwhj0cWKAuVo8nayXtNOLyZwvHRLCDDpgJOvsLxTzAsDXaZ2lxzzusD MFsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=yZLAlOhCGilIwuTmtW0IVLJRanvaBYIelqxDGN9L3mA=; b=ONKQ0g48P+KQJiG3ebzN1ZId1i2RtaA4PJUj6fWLzzr/E21RUBx2HOmPcnPCwbl2Yx OrrDgHjyQAxPizBTdS0IMPsU6Jc8tEJ2DcqR6YE2rfmIZ4ksv6Thkw0hM2rRXi5Igb28 ycyKjrXhu/CPobfTdxWfhyIZCb6PdGYcnBH9zl4obyfflmCcaqUM0TNTpzrPC6UZi9ah ijoeym7qPpsRRMqdsmAkMqmS5GYv5bD8RzCPGUYfil7FjRn5JNAj6kk6GRLSn9M3+Sax h5wRlMHy5GSnybx0nBxyA7SqfII88jSeK5ljPAO8/z4BZXsyxqg7I/+0s3IKa5+DVJUH 2Afg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ntlPTMV4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o22-20020a170906975600b007ae8d01144dsi893474ejy.717.2022.12.22.08.45.19; Thu, 22 Dec 2022 08:45:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ntlPTMV4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S230178AbiLVQnB (ORCPT + 99 others); Thu, 22 Dec 2022 11:43:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229603AbiLVQm5 (ORCPT ); Thu, 22 Dec 2022 11:42:57 -0500 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAC5227B0F; Thu, 22 Dec 2022 08:42:56 -0800 (PST) Received: by mail-wm1-x32d.google.com with SMTP id ja17so1830869wmb.3; Thu, 22 Dec 2022 08:42:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=yZLAlOhCGilIwuTmtW0IVLJRanvaBYIelqxDGN9L3mA=; b=ntlPTMV4mBJYN7GB1xLqSC6qEFW7/c4RVIUl7rVnZDNgEyUnIFTrW7ehcuwSjA9MOT roXNazIIcGsS24FHx1L0hDGQS3Z8bXmRO0K8a7JuCtbpCxERvYemdLj3Ua0F+5P3X8Js 6+6krr8AjM5RIrp+OxufDGOxjY2Hieowj0//MgfhL0H9x7rknnumipH69NwM/Hw9X11p peaLgdBwcc1r1HCNdLU7G+QzpckOgMmxMzZq1aEn04PkaKZum8VZr5h/v5z0CMpwNa1w EefqCy5z9v4qYICYsn3Qf9hH91DI57OppL3On7C7ra5J+d/YImz3xvg16yTkqmmOGHxI 5q1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yZLAlOhCGilIwuTmtW0IVLJRanvaBYIelqxDGN9L3mA=; b=Jwkc08Ugqo7++opMPsfC+9JfzLoqgu2ouvyVncv0Tju0zbLIjKAeItD/32IWSzClAz hCK6X/qetjYvGIo7JQe7zzzu8Hz7d/hOuzDLdTaypT5GE9oVfQ9ufxNfCkVnIskT5uYv mOcrs+pkT12kOsIaWn5MvI/UW+Z//kQA6Balb+NvVQqjH+SE7sVUO+FPwzPuK+IizkpK NcAepCfnCVuOPrWuAlVl2d6Oya9mqnUM5uq8F3vT5mnEDVE3psm+xFILhMha/aSDa/p/ J+RwpCvumVWvJaSNwolHDj/2BoBG0ixPNN8FgiBZzBq5NIElpS3INghVtizvRFxslmk6 nSbQ== X-Gm-Message-State: AFqh2kqbItKmIqwS2r3PG/DFksAVLfwSgLZRKbJahtUQBVGQF6EyL/L/ QoiqA9wZQA/6DKeXkKDu8Q== X-Received: by 2002:a05:600c:4d25:b0:3d2:27ba:dde0 with SMTP id u37-20020a05600c4d2500b003d227badde0mr4876976wmp.33.1671727375391; Thu, 22 Dec 2022 08:42:55 -0800 (PST) Received: from p183 ([46.53.253.211]) by smtp.gmail.com with ESMTPSA id t184-20020a1c46c1000000b003b4a699ce8esm6333221wma.6.2022.12.22.08.42.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Dec 2022 08:42:54 -0800 (PST) Date: Thu, 22 Dec 2022 19:42:53 +0300 From: Alexey Dobriyan To: Alejandro Colomar , Michael Kerrisk Cc: linux-kernel@vger.kernel.org, linux-man@vger.kernel.org, oss-security@lists.openwall.com Subject: [patch] proc.5: tell how to parse /proc/*/stat correctly Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752933380012243406?= X-GMAIL-MSGID: =?utf-8?q?1752933380012243406?= /proc/*/stat can't be parsed with split() or split(" ") or split(' ') or sscanf("%d (%s) ...") or equivalents because "comm" can contain whitespace and parenthesis and is not escaped by the kernel. BTW escaping would not help with naive split() anyway. Mention strrchr(')') so people can at least stop adding new bugs. Signed-off-by: Alexey Dobriyan --- man5/proc.5 | 5 +++++ 1 file changed, 5 insertions(+) --- a/man5/proc.5 +++ b/man5/proc.5 @@ -2092,6 +2092,11 @@ Strings longer than .B TASK_COMM_LEN (16) characters (including the terminating null byte) are silently truncated. This is visible whether or not the executable is swapped out. + +Note that \fIcomm\fP can contain space and closing parenthesis characters. +Parsing /proc/${pid}/stat with split() or equivalent, or scanf(3) isn't +reliable. The correct way is to locate closing parenthesis with strrchr(')') +from the end of the buffer and parse integers from there. .TP (3) \fIstate\fP \ %c One of the following characters, indicating process state: