Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp852262pxk; Thu, 17 Sep 2020 19:05:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyfQCNNvwln+R0QA/S9EPirZ2fZDrgVmwzMzsr9/IenJkO5hdw9YWkfhnghPp5Ige+uRdug X-Received: by 2002:aa7:dd16:: with SMTP id i22mr36623495edv.335.1600394750059; Thu, 17 Sep 2020 19:05:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600394750; cv=none; d=google.com; s=arc-20160816; b=H303HGoyynf7akyQdyzZcyJ/cl3B+JsdufqyedKt5ce5YYTDq4WZ1j98B1uPUxhd9s c1if8MCc/9r7SgpkA7sYMvc3lddxLlYiO31njwku515UdeJFP6Xsm7azQ2ra3eJK/s4C PuMNg9wYTJLVg9vg/hY1n3f7f3RbfFAXMW2hH+dVJQtbXAMXIRPhznKk6GYSiyIW/Dc/ r2EZNelZjTO4025/zf1dVeFq+++7cex8vdeIyk1vsOhpPat4pp6krCvbuBtkBI56xfTV SLZKtZZnX+Qc1XbUVZLm5kptOz+zbw89qqd41pGrIyMyczAmFVoKf6z8DBNP5HPryEfv 6Ugg== 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=Nw/HdEXyJ0vghV+QqcuzmTvT2vNgJ20VBYCTP5oTjzk=; b=ncB704f+S4UNkqHTO3mh23CopFiXQ4NL8ZxrSUVfsOIeuUsIpedyDv+urZ4h/S6SAQ ydaa9uzPHheEs6fatv+JNrCiJYVhbBLT/J9Vgcu984Vmt08EFVPNZrnFOMvbkMSqfplX xA423dAdUn+PyRtCsskxl4y0shbo+ZNs3h0SmLzCkf8bTWd5lZyU1B940p6d6TYr2ejN ycUp8MxGPWjlPVQUMql5w9E8o27qgnW7R37WG6Btl4GpDQtAoKiUAicHuU0VIECBNQ9F qpMR7vqDjrneve+EET4P+aHkUcT68iMPYfYhhoMlCQeYjFMQa8GtnjhxOhfdhTxiOLLX tQTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=uWL7aIC1; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v10si1235016ejx.195.2020.09.17.19.05.26; Thu, 17 Sep 2020 19:05:50 -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=@kernel.org header.s=default header.b=uWL7aIC1; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727086AbgIRCD3 (ORCPT + 99 others); Thu, 17 Sep 2020 22:03:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:49664 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726174AbgIRCD0 (ORCPT ); Thu, 17 Sep 2020 22:03:26 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D07D7208DB; Fri, 18 Sep 2020 02:03:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394606; bh=TO82qSTwghpzPIDY2QbkrSWqxVgQoKXVPz9boRqbIXM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uWL7aIC1lp3jPzKxqD/l+GKDkgSrkMdK+TYZOyjDIE3ZlvEwK0uz3EnyHCBS5YZfR fLTHEqA3o1CiHqPGJrILrnFePZk8mcbEE1c4ILrMsKGoUpQN6V/h/hjTjhCC+h2Vl5 DqLZxTZuTjYwd3ZvA+HrZBxv0qN/D5NZ01RQGbKc= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Vasily Averin , Andrew Morton , Jann Horn , Alexander Viro , Kees Cook , Hugh Dickins , Linus Torvalds , Sasha Levin , linux-mm@kvack.org Subject: [PATCH AUTOSEL 5.4 112/330] mm/swapfile.c: swap_next should increase position index Date: Thu, 17 Sep 2020 21:57:32 -0400 Message-Id: <20200918020110.2063155-112-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Vasily Averin [ Upstream commit 10c8d69f314d557d94d74ec492575ae6a4f1eb1c ] If seq_file .next fuction does not change position index, read after some lseek can generate unexpected output. In Aug 2018 NeilBrown noticed commit 1f4aace60b0e ("fs/seq_file.c: simplify seq_file iteration code and interface") "Some ->next functions do not increment *pos when they return NULL... Note that such ->next functions are buggy and should be fixed. A simple demonstration is dd if=/proc/swaps bs=1000 skip=1 Choose any block size larger than the size of /proc/swaps. This will always show the whole last line of /proc/swaps" Described problem is still actual. If you make lseek into middle of last output line following read will output end of last line and whole last line once again. $ dd if=/proc/swaps bs=1 # usual output Filename Type Size Used Priority /dev/dm-0 partition 4194812 97536 -2 104+0 records in 104+0 records out 104 bytes copied $ dd if=/proc/swaps bs=40 skip=1 # last line was generated twice dd: /proc/swaps: cannot skip to specified offset v/dm-0 partition 4194812 97536 -2 /dev/dm-0 partition 4194812 97536 -2 3+1 records in 3+1 records out 131 bytes copied https://bugzilla.kernel.org/show_bug.cgi?id=206283 Link: http://lkml.kernel.org/r/bd8cfd7b-ac95-9b91-f9e7-e8438bd5047d@virtuozzo.com Signed-off-by: Vasily Averin Reviewed-by: Andrew Morton Cc: Jann Horn Cc: Alexander Viro Cc: Kees Cook Cc: Hugh Dickins Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/swapfile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 891a3ef486511..646fd0a8e3202 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2737,10 +2737,10 @@ static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) else type = si->type + 1; + ++(*pos); for (; (si = swap_type_to_swap_info(type)); type++) { if (!(si->flags & SWP_USED) || !si->swap_map) continue; - ++*pos; return si; } -- 2.25.1