public inbox for linux-man@vger.kernel.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx.manpages@gmail.com>
To: Mingye Wang <arthur200126@gmail.com>
Cc: linux-man@vger.kernel.org, siddhesh@gotplt.org,
	DJ Delorie <dj@redhat.com>, Florian Weimer <fweimer@redhat.com>,
	Andreas Schwab <schwab@linux-m68k.org>,
	Zack Weinberg <zack@owlfolio.org>, Sam James <sam@gentoo.org>,
	Wilco Dijkstra <Wilco.Dijkstra@arm.com>,
	Adhemerval Zanella <adhemerval.zanella@linaro.org>
Subject: Re: [RFC PATCH] malloc_usable_size.3: Warn about _FORTIFY_SOURCE interaction
Date: Wed, 5 Apr 2023 04:41:56 +0200	[thread overview]
Message-ID: <616e47e3-c09a-26cf-59aa-e38bbdde96ca@gmail.com> (raw)
In-Reply-To: <CAD66C+YQKWJQNv2i=8+BuL3Z5NzDQsG-1izhVxZ549xhMTTUjA@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 2520 bytes --]

Hi Mingye,

On 4/4/23 07:52, Mingye Wang wrote:
> Hi all,
> 
> In (somewhat) recent discussions about _FORTIFY_SOURCE level 3, a
> common snag to hit seems to be abuse of malloc_usable_size(). The
> attached patch is my attempt at making the situation easier to sort
> through.
> 
> See siddhesh's comment on GitHub.[0] I wonder if the language needs to
> be stronger.
>   [0]: https://github.com/systemd/systemd/issues/22801#issuecomment-1343041481
> 
> Best,
> Mingye Wang (Artoria2e5)


> From f061522764ec417e80622db557853c2d7493bbb7 Mon Sep 17 00:00:00 2001
> From: Mingye Wang <arthur200126@gmail.com>
> Date: Tue, 4 Apr 2023 13:43:39 +0800
> Subject: [PATCH] malloc_usable_size.3: Warn about _FORTIFY_SOURCE interaction
> 
> Abuse of malloc_usable_size() is common enough to snap up Redhat's
> trials of -D_FORTIFY_SOURCE=3.  Warn against this to ease debugging.
> 
> Signed-Off-by: Mingye Wang <arthur200126@gmail.com>
> ---
>  man3/malloc_usable_size.3 | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/man3/malloc_usable_size.3 b/man3/malloc_usable_size.3
> index 754b255de..1361e6f1e 100644
> --- a/man3/malloc_usable_size.3
> +++ b/man3/malloc_usable_size.3
> @@ -62,5 +62,14 @@ the number of excess bytes in an allocation depends on
>  the underlying implementation.
>  .PP
>  The main use of this function is for debugging and introspection.
> +.PP
> +.BR Warning :
> +Some programs abuse
> +.BR malloc_usable_size ()
> +to reduce the number of calls to
> +.BR realloc (3).
> +Such use will confuse
> +.BR _FORTIFY_SOURCE
> +level 3, as it only keeps track of the original requested size.

This is much milder than what I read in the linked discussions.
I would go ahead with something much stronger as suggested there,
or did you change your mind?

How about using DJ's suggestion in a CAVEATS section (I cut the
clause in the paragraph)?

    The value returned by malloc_usable_size() may be greater than the
    requested size of the allocation because of various internal
    implementation details, none of which the programmer should rely on.
    This function is intended to only be used for diagnostics and
    statistics; writing to the excess memory without first calling
    realloc() to resize the allocation is not supported.  The returned
    value is only valid at the time of the call.

Cheers,
Alex

-- 
<http://www.alejandro-colomar.es/>
GPG key fingerprint: A9348594CE31283A826FBDD8D57633D441E25BB5

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

      parent reply	other threads:[~2023-04-05  2:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-04  5:52 [RFC PATCH] malloc_usable_size.3: Warn about _FORTIFY_SOURCE interaction Mingye Wang
2023-04-04 11:42 ` Siddhesh Poyarekar
2023-04-05  0:51   ` Sam James
2023-04-05  2:35   ` Alejandro Colomar
2023-04-05 12:58     ` Siddhesh Poyarekar
2023-04-05  2:41 ` Alejandro Colomar [this message]

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=616e47e3-c09a-26cf-59aa-e38bbdde96ca@gmail.com \
    --to=alx.manpages@gmail.com \
    --cc=Wilco.Dijkstra@arm.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=arthur200126@gmail.com \
    --cc=dj@redhat.com \
    --cc=fweimer@redhat.com \
    --cc=linux-man@vger.kernel.org \
    --cc=sam@gentoo.org \
    --cc=schwab@linux-m68k.org \
    --cc=siddhesh@gotplt.org \
    --cc=zack@owlfolio.org \
    /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