All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: Thomas Haller <thaller@redhat.com>
Cc: NetFilter <netfilter-devel@vger.kernel.org>
Subject: Re: [PATCH nft 5/8] src: rework SNPRINTF_BUFFER_SIZE() and avoid "-Wunused-but-set-variable"
Date: Mon, 28 Aug 2023 21:53:04 +0200	[thread overview]
Message-ID: <ZOz7IMG0J1B0HVlB@calendula> (raw)
In-Reply-To: <31efcb8e9ceac6f71003abd9517cca981550fc91.camel@redhat.com>

On Mon, Aug 28, 2023 at 06:45:00PM +0200, Thomas Haller wrote:
> On Mon, 2023-08-28 at 18:04 +0200, Pablo Neira Ayuso wrote:
> > On Mon, Aug 28, 2023 at 05:49:53PM +0200, Thomas Haller wrote:
> > > On Mon, 2023-08-28 at 17:13 +0200, Pablo Neira Ayuso wrote:
> > 
> > > SNPRINTF_BUFFER_SIZE() rejects truncation of the string by
> > > asserting
> > > against it. That behavior is part of the API of that function.
> > > Error
> > > checking after an assert seems unnecessary.
> > > 
> > > The check "if (len == NF_LOG_PREFIXLEN)" seems wrong anyway. After
> > > truncation, "len" would be zero. The code previously checked
> > > whether
> > > nothing was appended, but the error string didn't match that
> > > situation.
> > > 
> > > Maybe SNPRINTF_BUFFER_SIZE() should not assert against truncation?
> > 
> > IIRC, the goal for this function was to handle snprintf() and all its
> > corner cases. If there is no need for it or a better way to do this,
> > this is welcome.
> > 
> 
> I think the macro is sensible (at least, after some cleanup).
> 
> It makes a choice, that the caller must ensure a priori that the buffer
> is long enough (by asserting).
> 
> By looking at the callers, it's not clear to me, whether the callers
> can always ensure that.  For meta_key_parse(), it seems the maximum
> string is limited by meta_templates. But for stmt_evaluate_log_prefix()
> it may be possible to craft user-input that triggers the assertion,
> isn't it?
> 
> Maybe the macro and the callers should anticipate and handle
> truncation?

This should not silently truncate strings, instead bail out to user in
case the string is too long.

#define NF_LOG_PREFIXLEN       128

Maximum length as specified by uapi/linux/netfilter/nf_log.h

Currently in userspace, if I specify a string longest than that, I can
see ASAN complains on incorrect memory management from userspace
(without your patchset), so this code is currently broken (by me
mostly likely since I was the last one to touch those bits).

While at this, it would be good to fix it and add a test to cover
maximum prefix length.

Regarding meta_key_parse(), these days the preferred approach is to
use start conditions in flex. This function should go away, it was an
early attempt to reduce tokens by using STRING from bison, which
turned out to be flawed.

It is not worth to fix meta_key_parse(), flex start conditions and
bison parser should be used.

This macro is hiding behind a bit of technical debt.

  reply	other threads:[~2023-08-28 19:54 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-28 14:43 [PATCH nft 0/8] fix compiler warnings with clang Thomas Haller
2023-08-28 14:43 ` [PATCH nft 1/8] netlink: avoid "-Wenum-conversion" warning in dtype_map_from_kernel() Thomas Haller
2023-08-28 14:43 ` [PATCH nft 2/8] netlink: avoid "-Wenum-conversion" warning in parser_bison.y Thomas Haller
2023-08-28 14:43 ` [PATCH nft 3/8] src: use "%zx" format instead of "%Zx" Thomas Haller
2023-08-28 14:43 ` [PATCH nft 4/8] datatype: avoid cast-align warning with struct sockaddr result from getaddrinfo() Thomas Haller
2023-08-28 14:43 ` [PATCH nft 5/8] src: rework SNPRINTF_BUFFER_SIZE() and avoid "-Wunused-but-set-variable" Thomas Haller
2023-08-28 15:13   ` Pablo Neira Ayuso
2023-08-28 15:49     ` Thomas Haller
2023-08-28 16:04       ` Pablo Neira Ayuso
2023-08-28 16:45         ` Thomas Haller
2023-08-28 19:53           ` Pablo Neira Ayuso [this message]
2023-08-29 13:01             ` Thomas Haller
2023-08-28 14:43 ` [PATCH nft 6/8] src: suppress "-Wunused-but-set-variable" warning with "parser_bison.c" Thomas Haller
2023-08-28 14:43 ` [PATCH nft 7/8] utils: add _NFT_PRAGMA_WARNING_DISABLE()/_NFT_PRAGMA_WARNING_REENABLE helpers Thomas Haller
2023-08-28 14:43 ` [PATCH nft 8/8] datatype: suppress "-Wformat-nonliteral" warning in integer_type_print() Thomas Haller
2023-08-28 15:08   ` Pablo Neira Ayuso
2023-08-28 15:33     ` Thomas Haller
2023-08-28 15:54       ` Pablo Neira Ayuso
2023-08-28 16:24         ` Thomas Haller

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=ZOz7IMG0J1B0HVlB@calendula \
    --to=pablo@netfilter.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=thaller@redhat.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.