From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (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 01538305688 for ; Fri, 7 Aug 2026 03:31:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786073506; cv=none; b=fnzIq0+ghHEMRpkHqvHVJKj6FAXQDiR0Zc/4/N+DrL9yKDPwHd9qbhFyGWgPX9YoZpyE+uB/hCTXsYSQoE+etwmMQZQVc9qsgukk55rKYGd/daO/L+ufDrtliAOr/hkMjHGAFegoYD6lZUR5OHXqlfre8xFm1/Dcy/dEQe3Q3gw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786073506; c=relaxed/simple; bh=uftTTtmCtTyPGczJJxoLHYfY8QhRqsceiesrkwVpR+g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VaUWo4STc3xZqNx2bMssDCe89K5R6QZTsLubbuJt2e3N0GVAX3n4IkCdUMDP1NiTHWOGoTPt67UPNvxmLKn3wNtpzh8+Hslzb55KYeLcuq8B0aez3QRdRKEETWOahEPASyoPHPBgPnDhvVW1A2U3uV9zPht2+wvnb5rpyiyWikA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: 7e894400921011f1aa26b74ffac11d73-20260807 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.12,REQID:33ea77ee-d0f3-4b6e-9568-4b9186e6dcaa,IP:0,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION: release,TS:0 X-CID-META: VersionHash:e7bac3a,CLOUDID:30d4aacd81b1023fa7802ba724ca08a9,BulkI D:nil,BulkQuantity:0,Recheck:0,SF:81|82|102|865|898,TC:nil,Content:0|15|50 ,EDM:-3,IP:nil,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,O SA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 7e894400921011f1aa26b74ffac11d73-20260807 X-User: liuxixin@kylinos.cn Received: from [127.0.1.1] [(10.44.16.150)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA with TLSv1.3 TLS_AES_256_GCM_SHA384 256/256) with ESMTP id 1171004808; Fri, 07 Aug 2026 11:31:36 +0800 From: Xixin Liu To: linuxppc-dev@lists.ozlabs.org Cc: maddy@linux.ibm.com, mpe@ellerman.id.au, npiggin@gmail.com, chleroy@kernel.org, linux-kernel@vger.kernel.org, liuxixin@kylinos.cn Subject: [PATCH v1 3/4] powerpc/pseries: energy: bound H_BEST_ENERGY cnt and sysfs output Date: Fri, 07 Aug 2026 11:11:14 +0800 Message-ID: In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Mailer: patches/scripts/send-local.py The H_BEST_ENERGY sysfs path takes cnt from the hypercall return buffer and walks buf_page[2*i+1] without checking that cnt fits in the single page allocated for the hcall, and sprintf()s into the PAGE_SIZE sysfs buffer without remaining-space checks. Clamp cnt to the number of u32 pairs that fit in the page, and stop formatting before overflowing the sysfs page. Signed-off-by: Xixin Liu --- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/arch/powerpc/platforms/pseries/pseries_energy.c b/arch/powerpc/platforms/pseries/pseries_energy.c index fdaf85ecd39b..8344f00656e5 100644 --- a/arch/powerpc/platforms/pseries/pseries_energy.c +++ b/arch/powerpc/platforms/pseries/pseries_energy.c @@ -209,16 +209,28 @@ return -EINVAL; } + /* + * Each entry occupies two u32s in buf_page. Never walk past the + * page, and never sprintf past the PAGE_SIZE sysfs buffer. + */ cnt = retbuf[0]; + if (cnt > (PAGE_SIZE / sizeof(u32)) / 2) + cnt = (PAGE_SIZE / sizeof(u32)) / 2; + for (i = 0; i < cnt; i++) { cpu = drc_index_to_cpu(buf_page[2*i+1]); if ((cpu_online(cpu) && !activate) || - (!cpu_online(cpu) && activate)) + (!cpu_online(cpu) && activate)) { + if (s - page >= PAGE_SIZE - 16) + break; s += sprintf(s, "%d,", cpu); + } } if (s > page) { /* Something to show */ s--; /* Suppress last comma */ - s += sprintf(s, "\n"); + /* sprintf needs room for '\n' and trailing NUL. */ + if (s - page < PAGE_SIZE - 1) + s += sprintf(s, "\n"); } free_page((unsigned long) buf_page); -- 2.43.0