Linux s390 Architecture development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alexander Egorenkov" <egorenar@linux.ibm.com>
Cc: "Heiko Carstens" <hca@linux.ibm.com>,
	"Alexander Gordeev" <agordeev@linux.ibm.com>,
	"Christian Borntraeger" <borntraeger@linux.ibm.com>,
	linux-s390@vger.kernel.org, "Vasily Gorbik" <gor@linux.ibm.com>
Subject: Re: [PATCH v5 1/4] s390/sclp: Introduce macro sclp_gds_for_each()
Date: Thu, 17 Sep 2026 07:08:50 +0000	[thread overview]
Message-ID: <20260917070850.B5E171F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260917065824.2858737-2-egorenar@linux.ibm.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] sclp: out-of-bounds read in sclp_gds_for_each() due to integer promotion
--

commit f292c3c2b28abf9088d99e5a8af81f738a00c9ee
Author: Alexander Egorenkov <egorenar@linux.ibm.com>

s390/sclp: Introduce macro sclp_gds_for_each()

This commit introduces a macro to iterate safely over GDS {sub}vectors. It
adds bounds checks to avoid infinite loops and out-of-bounds memory accesses
when parsing malformed event buffers.

> diff --git a/drivers/s390/char/sclp.h b/drivers/s390/char/sclp.h
> index b31a680e08714..d22003b769f14 100644
> --- a/drivers/s390/char/sclp.h
> +++ b/drivers/s390/char/sclp.h
> @@ -360,25 +360,33 @@ sclp_ascebc_str(char *str, int nr)
>  	(machine_is_vm()) ? ASCEBC(str, nr) : ASCEBC_500(str, nr);
>  }
>  
> -static inline struct gds_vector *
> -sclp_find_gds_vector(void *start, void *end, u16 id)
> +/* Loop over all GDS {sub}vectors in a safe manner. */
> +#define sclp_gds_for_each(v, n, start, end)	\
> +	for ((n) = (end) - (start), (v) = (start);	\
> +	     (n) >= sizeof(*(v)) && (v)->length >= sizeof(*(v)) && (v)->length <= (n);	\

[Severity: High]
Does this code introduce an out-of-bounds read due to integer promotion?

If a malformed SCLP event buffer is provided by the hardware with an
evbuf->length smaller than the header size, start will be greater than end.
This happens in callers like sclp_ocf_handler() and sclp_tty_receiver():

drivers/s390/char/sclp_ocf.c:sclp_ocf_handler() {
    ...
    v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length,
                             0x9f00);
    ...
}

In sclp_gds_for_each(), the loop limit n is computed as (end) - (start).
Because callers define n as an int, if start is greater than end, n will be a
negative integer.

The bounds condition (n) >= sizeof(*(v)) compares the signed int n with the
unsigned size_t returned by sizeof. Due to C integer promotion rules, n is
implicitly promoted to an unsigned type, wrapping the negative value to a very
large positive number.

Consequently, the bounds check evaluates to true, and the loop mistakenly
executes (v)->length >= sizeof(*(v)), reading from the out-of-bounds start
pointer.

The original logic ((void *) v < end) handled start > end safely by
immediately skipping the loop.

> +	     (n) -= (v)->length, (v) = (void*)(v) + (v)->length)
> +
> +static inline struct gds_vector *sclp_find_gds_vector(void *start,
> +						      void *end, u16 id)
>  {
>  	struct gds_vector *v;
> -
> -	for (v = start; (void *) v < end; v = (void *) v + v->length)
> +	int n;
> +	sclp_gds_for_each(v, n, start, end) {
>  		if (v->gds_id == id)
>  			return v;
> +	}
>  	return NULL;
>  }

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260917065824.2858737-1-egorenar@linux.ibm.com?part=1

  reply	other threads:[~2026-09-17  7:08 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-17  6:58 [PATCH v5 0/4] s390/sclp: Misc fixes Alexander Egorenkov
2026-09-17  6:58 ` [PATCH v5 1/4] s390/sclp: Introduce macro sclp_gds_for_each() Alexander Egorenkov
2026-09-17  7:08   ` sashiko-bot [this message]
2026-09-17  9:49     ` Alexander Egorenkov
2026-09-17 12:32   ` Peter Oberparleiter
2026-09-17 13:21     ` Alexander Egorenkov
2026-09-17  6:58 ` [PATCH v5 2/4] s390/sclp_tty: Make use of sclp_gds_for_each() Alexander Egorenkov
2026-09-17  7:12   ` sashiko-bot
2026-09-17  6:58 ` [PATCH v5 3/4] s390/sclp_ocf: Fix computation of length of GDS values Alexander Egorenkov
2026-09-17  7:09   ` sashiko-bot
2026-09-17 12:50   ` Peter Oberparleiter
2026-09-17  6:58 ` [PATCH v5 4/4] s390/sclp: Ensure no callback gets called after sclp_unregister() returns Alexander Egorenkov
2026-09-17  7:07   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260917070850.B5E171F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=egorenar@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=linux-s390@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox