From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE96C45090A for ; Fri, 22 May 2026 15:25:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779463528; cv=none; b=Cybs4tGcC4ouBYOzpvC3kQOlys/DUg4QpCbo5yeKZCWboJGr8x7oDwK2XC+st44EyIcn5ygP2HuthnABze+NLLJR/fzMCVROkfwMxexEypbfP57qa0La87HK7KdgZoJLrFHXoNtDH0JaitGwK8R1oEpbG3Rsu78F3ubU61zhX0A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779463528; c=relaxed/simple; bh=mgZDZFd/kftyaRkGd7GJZ68U/IMXfGzVEju4yiqnrFA=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=jmBn23P61xahq0KewbNRBlDHKvRAt7mZvdIMLonddgBY2FXSD8oZnrGfL2VZd8JdW3CrReJHwhrQQASI1oFonwi7wTWlWYITnQcmWqjaAGy5wne5PBPzCe3MjbofAeG86p2ibhKGMkOvAi1Acbc6ZuSFF/rGgBhUlpuBhha2EtY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ELKN+E8I; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ELKN+E8I" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-2ba1e9d3687so49002775ad.3 for ; Fri, 22 May 2026 08:25:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779463526; x=1780068326; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=sqJX3Syj5JCe19TOR61TPakqaL+M0rzoKj5PhYVwVRQ=; b=ELKN+E8IT1Upw8IemoPJqYzg74wqfdT9JZX9CV05vZ2e8/+axzlvAH1slEgFcgJE8T 5VAuYACwnPZwNcOs6y3/b5L5GQOdhX8wSvQLxIqC7lMSYaFj553Cbdqk8Wa9d4Y5eOjP ScUZ31rTDPS/eXcEDVZesCpQgwFzQFcxE6aszAWeFQnSAe+qfSfEq2wmu4kmauyDceMQ 6n/tAEutyHMWKWUaiknFZW31FPyqjj9vuKO5j8Z+Z4uPGsxI7yRAl27HOJCDjEvHBAHc Gr7DilvPB+rqq5zc8eoCflSNUyzEUrTfPV2pj3e5JYwwbA9pQ+IN3olKT5Co71B5g9zx MjnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779463526; x=1780068326; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=sqJX3Syj5JCe19TOR61TPakqaL+M0rzoKj5PhYVwVRQ=; b=ORRPglUKp3V6QYFJZJuYpgvnbK53LPCHf8HI4auX6w1aIr3v0YZF3qeOxiTl64lYXI DnmjqkFf9vMyk1qeh/CcM0Pc6oPlDDr0xlxThoH1TsCnjFuoOkkpT+Wbp3bDIe5/30jN sHqcFWD0Lis5ZZZzYnBX3whSMgDYaJ/QbGCdFWHbnfJSEqRuguy85JHg1/CYg49zy5o4 H2OmCZQhVS6NJ/w/k/QvUpn5jlfIvdQyrWshhfQUF1PUGSopTcN9BkjHcIJ4nMLt1rtk SvpFTTr7AG0ZyMcts7/3vtIBNR2yeGmfA56MdA41Pw7mALWc+wkVqwhhm5LCX0HccdaW LKAA== X-Forwarded-Encrypted: i=1; AFNElJ/sV+qkaaP3MsCod56h+CH4kU0KD6dignusu/niYO3dRVs9fs803ctrqIil0yYMAzOBt5537V3DpOHAFb0=@vger.kernel.org X-Gm-Message-State: AOJu0YxWzKlsqd2khhFjgzFZZanMyB3Wo5RgeZjyWCF1H+iXdL/0ZIeD RBhGX9p9rI6RtpeO3baKiGWZZCIZ9BmOB5SuJH3revnwYE6y1aCKwdPaT6fUZg== X-Gm-Gg: Acq92OFZ/l3YBMcgJ0uvx6s+H+7e4BU4k5OtMNyyjnMOvaVbqSPhIKwGKriMqodTzU0 HD0ciiPLvwp3xu3Uq1ez25SLXcWmG5kWGdP/Z7H1LoxuSJTXV+3Yk6BVxgIWTzmQPWw/xRiOF4B vPiq0J1n2w8myaa5k9hqfzZic+J4l83Y+AD6Oy4poccB7mmjnScTzcpbTd4bTPyYcMZkjTr0jqH lMkQDUviIYR6+tXVASOhbevhJhFrLLwYGbB3I+GEQebkE/+FQ6l7V+YiNxlxXaIOBOYYQ/5nVfX tmQXBXEL4aFuSY5tUWJg+rqH1jaSC0AyQ8hAqoaLJXo47Bx/RzdOcVZfX3+cXRmCx4ODimaGJmc IDZk7MdFrwRXXJnvYiK2nZ3js4IdKdkBlI83dmwyZhL64A0sHqrV/BrHJQXtDUpHXC3nzAAY+3d aCBl4glhiRm8QIW8WqBe6lZtmVg4HHFlifMXbVXRSmqzYxHz7R X-Received: by 2002:a17:903:1cd:b0:2b0:6f21:8289 with SMTP id d9443c01a7336-2beb067c5a6mr45393675ad.25.1779463526190; Fri, 22 May 2026 08:25:26 -0700 (PDT) Received: from csl-conti-dell7858.ntu.edu.sg ([155.69.195.57]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2beb58e4fcfsm20603405ad.71.2026.05.22.08.25.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 May 2026 08:25:25 -0700 (PDT) From: Maoyi Xie To: Vladimir Oltean Cc: Andrew Lunn , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: List iterator used after loop in sja1105_insert_gate_entry? Date: Fri, 22 May 2026 23:25:22 +0800 Message-Id: <20260522152522.112510-1-maoyixie.tju@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Hi all, I came across what looks like an iterator used after the loop ends in drivers/net/dsa/sja1105/sja1105_vl.c (linux-7.1-rc1), in sja1105_insert_gate_entry(). I would appreciate your input on whether this is worth fixing or whether I am misreading the pattern. list_for_each_entry(p, &gating_cfg->entries, list) { if (p->interval == e->interval) { NL_SET_ERR_MSG_MOD(extack, "Gate conflict"); rc = -EBUSY; goto err; } if (e->interval < p->interval) break; } list_add(&e->list, p->list.prev); When the loop walks the whole list without break (the new interval is larger than every existing one), `p` walks past the end of the list and `p->list` aliases the list head via container_of() offset cancellation. `p->list.prev` then resolves to head->prev, which is the real tail entry's list, and the list_add() inserts the new entry at the tail. That is the intended behaviour for the case where the loop falls through. The dereference of the iterator after the loop ends is the part I am unsure about. Same shape as the Koschel cleanups from 2022 (99d8ae4ec8a tracing, 2966a9918df clockevents, dc1acd5c946 dlm, and others) and the "controlled container confusion" pattern described in [1]. I drafted a candidate fix that initialises an explicit `insert_after` pointer to gating_cfg->entries.prev (the list tail) and overwrites it to p->list.prev only on early break, then passes insert_after to list_add(). The iterator is no longer dereferenced after the loop and the diff is 5+/2-. I built sja1105_vl.o on x86_64 with NET_DSA_SJA1105 + NET_DSA_SJA1105_TAS + NET_DSA_SJA1105_VL at W=1 and the object compiles clean with no warnings. I also ran a small userspace mock of the two versions across seven scenarios: single entry above and below the new interval, single entry conflict, multi-entry insert at head, middle, and tail (fall through), and a multi-entry mid-list conflict. The final list ordering and the return code match in every case. Does this look like something worth a [PATCH]? Happy to send one if so, or to drop it if the shape here is fine. Thanks, Maoyi https://maoyixie.com/ [1] Jakob Koschel et al., "UNCONTAINED: Uncovering Container Confusion in the Linux Kernel", USENIX Security 2023. https://www.usenix.org/conference/usenixsecurity23/presentation/koschel