From: Simon Horman <horms@kernel.org>
To: Arthur Kiyanovski <akiyano@amazon.com>
Cc: David Miller <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>,
Richard Cochran <richardcochran@gmail.com>,
netdev@vger.kernel.org, Eric Dumazet <edumazet@google.com>,
Paolo Abeni <pabeni@redhat.com>,
David Woodhouse <dwmw2@infradead.org>,
Thomas Gleixner <tglx@linutronix.de>,
Miroslav Lichvar <mlichvar@redhat.com>,
Andrew Lunn <andrew+netdev@lunn.ch>, Andrew Lunn <andrew@lunn.ch>,
Carolina Jubran <cjubran@nvidia.com>,
Wen Gu <guwen@linux.alibaba.com>,
Xuan Zhuo <xuanzhuo@linux.alibaba.com>,
David Woodhouse <dwmw@amazon.com>,
Yonatan Sarna <ysarna@amazon.com>,
Zorik Machulsky <zorik@amazon.com>,
Alexander Matushevsky <matua@amazon.com>,
Saeed Bshara <saeedb@amazon.com>, Matt Wilson <msw@amazon.com>,
Anthony Liguori <aliguori@amazon.com>,
Nafea Bshara <nafea@amazon.com>,
Evgeny Schmeilin <evgenys@amazon.com>,
Netanel Belgazal <netanel@amazon.com>,
Ali Saidi <alisaidi@amazon.com>,
Benjamin Herrenschmidt <benh@amazon.com>,
Noam Dagan <ndagan@amazon.com>,
David Arinzon <darinzon@amazon.com>,
Evgeny Ostrovsky <evostrov@amazon.com>,
Ofir Tabachnik <ofirt@amazon.com>,
Amit Bernstein <amitbern@amazon.com>,
linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org,
shuah@kernel.org, Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>,
Bjorn Helgaas <bhelgaas@google.com>,
vadim.fedorenko@linux.dev, David Woodhouse <dwmw@amazon.co.uk>
Subject: Re: [PATCH v6 net-next 4/7] ptp: ptp_vmclock: Implement attributes ioctls
Date: Tue, 4 Aug 2026 13:37:39 +0100 [thread overview]
Message-ID: <20260804123739.GM51943@horms.kernel.org> (raw)
In-Reply-To: <20260729233740.16516-5-akiyano@amazon.com>
This is an AI-generated review of your patch. The human sending this
email has considered the AI review valid, or at least plausible.
Full review at: https://sashiko.dev/#/patchset/20260729233740.16516-2-akiyano@amazon.com?part=4
---
--- Patch [4]: [PATCH v6 net-next 4/7] ptp: ptp_vmclock: Implement attributes ioctls ---
commit 9ab73b649349967c6e3befcbed4c125d48bd8c43
Author: Arthur Kiyanovski <akiyano@amazon.com>
ptp: ptp_vmclock: Implement attributes ioctls
Implement the gettimexattrs64 and getcrosststampattrs callbacks in the
ptp_vmclock driver to provide clock quality attributes through the new
PTP_SYS_OFFSET_EXTENDED_ATTRS and PTP_SYS_OFFSET_PRECISE_ATTRS ioctls.
> diff --git a/drivers/ptp/ptp_vmclock.c b/drivers/ptp/ptp_vmclock.c
> index eebdcd5ebc080..fe26123ad940d 100644
> --- a/drivers/ptp/ptp_vmclock.c
> +++ b/drivers/ptp/ptp_vmclock.c
> @@ -95,13 +106,111 @@ static bool tai_adjust(struct vmclock_abi *clk, uint64_t *sec)
[ ... ]
> +static void vmclock_populate_ptp_attributes(struct vmclock_state *st,
> + struct ptp_clock_attrs *att,
> + uint64_t delta)
> +{
> + uint64_t maxerror_ns = UINT_MAX;
> +
> + if (!att)
> + return;
> +
> + /* Only calculate if the base error is flagged as valid
> + * by the hypervisor.
> + */
> + if (VMCLOCK_FIELD_PRESENT(st->clk, time_maxerror_nanosec) &&
> + (le64_to_cpu(st->clk->flags) & VMCLOCK_FLAG_TIME_MAXERROR_VALID)) {
> + maxerror_ns = le64_to_cpu(st->clk->time_maxerror_nanosec);
> +
> + /* If frequency error is also valid, accumulate it
> + * over the delta.
> + */
> + if (VMCLOCK_FIELD_PRESENT(st->clk, counter_period_maxerror_rate_frac_sec) &&
> + (le64_to_cpu(st->clk->flags) & VMCLOCK_FLAG_PERIOD_MAXERROR_VALID)) {
> + uint64_t maxerror_rate, err_hi, err_frac, growth_ns;
> +
> + if (st->clk->counter_period_shift >= 128) {
> + maxerror_ns = U64_MAX;
> + goto saturate;
> + }
> +
> + maxerror_rate = le64_to_cpu(st->clk->counter_period_maxerror_rate_frac_sec);
> + err_frac = mul_u64_u64_shr_add_u64(&err_hi, delta,
> + maxerror_rate,
> + st->clk->counter_period_shift,
> + 0);
[Severity: High]
Could a malicious hypervisor modify counter_period_shift concurrently?
The counter_period_shift field is read directly from shared memory twice,
first for the bounds check and then as an argument to
mul_u64_u64_shr_add_u64(). If the value changes between these reads, it
could bypass the bounds check and cause an out-of-bounds shift.
Would it be better to read it once using READ_ONCE() to ensure the bounds
check operates on the exact value passed to the math helper?
> +
> + if (err_hi >= U64_MAX / NSEC_PER_SEC) {
> + maxerror_ns = U64_MAX;
> + goto saturate;
> + }
> +
> + growth_ns = (err_hi * NSEC_PER_SEC) +
> + mul_u64_u64_shr(err_frac, NSEC_PER_SEC, 64);
> +
> + /* Guard against overflow */
> + if (U64_MAX - growth_ns < maxerror_ns)
> + maxerror_ns = U64_MAX;
> + else
> + maxerror_ns += growth_ns;
> + }
> + }
> +
> +saturate:
> + /* PTP UAPI error_bound is 32-bit nanoseconds */
> + att->error_bound = (maxerror_ns > UINT_MAX) ?
> + UINT_MAX : (uint32_t)maxerror_ns;
> + att->valid |= PTP_ATTRS_VALID_ERROR_BOUND;
[Severity: Medium]
Does this unconditionally tell userspace that the error bound is valid, even
if the hypervisor flagged time_maxerror_nanosec as invalid?
If VMCLOCK_FLAG_TIME_MAXERROR_VALID is clear, maxerror_ns remains UINT_MAX
and PTP_ATTRS_VALID_ERROR_BOUND is still set. Userspace might interpret the
resulting UINT_MAX as a legitimate worst-case error bound rather than an
unsupported attribute.
> + att->timescale = vmclock_get_ptp_timescale(st->clk->time_type);
> + /* tai_adjust() already converted UTC to TAI before we're called */
> + if (st->clk->time_type == VMCLOCK_TIME_UTC)
> + att->timescale = PTP_TIMESCALE_TAI;
> + att->valid |= PTP_ATTRS_VALID_TIMESCALE;
> + att->status = vmclock_get_ptp_status(st->clk->clock_status);
> + att->valid |= PTP_ATTRS_VALID_STATUS;
> +}
> +
> static int vmclock_get_crosststamp(struct vmclock_state *st,
> struct ptp_system_timestamp *sts,
> struct system_counterval_t *system_counter,
[ ... ]
> delta = cycle - le64_to_cpu(st->clk->counter_value);
>
> frac_sec = mul_u64_u64_shr_add_u64(&tspec->tv_sec, delta,
> le64_to_cpu(st->clk->counter_period_frac_sec),
> st->clk->counter_period_shift,
> le64_to_cpu(st->clk->time_frac_sec));
[Severity: High]
This is a pre-existing issue, but can counter_period_shift be greater than
or equal to 128 here?
Since counter_period_shift is controlled by the hypervisor and passed directly
to mul_u64_u64_shr_add_u64() without bounds checking, an invalid value could
cause an out-of-bounds bitwise shift and undefined behavior:
mul_u64_u64_shr_add_u64() {
...
res >>= shift;
...
}
Given that vmclock_populate_ptp_attributes() actively defends against
invalid shift values, should we add a similar bounds check here to prevent
a potential kernel panic?
> tspec->tv_nsec = mul_u64_u64_shr(frac_sec, NSEC_PER_SEC, 64);
> tspec->tv_sec += le64_to_cpu(st->clk->time_sec);
>
> if (!tai_adjust(st->clk, &tspec->tv_sec))
> return -EINVAL;
>
> + /*
> + * Capture clock state inside the seq_count loop for a
> + * consistent snapshot with the timestamp. The attrs path
> + * reports it to userspace via the status field; the legacy
> + * path saves it for the UNRELIABLE check after the loop.
> + */
next prev parent reply other threads:[~2026-08-04 12:37 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-29 23:37 [PATCH v6 net-next 0/7] ptp: Add PHC timestamp quality attributes Arthur Kiyanovski
2026-07-29 23:37 ` [PATCH v6 net-next 1/7] ptp: Add ioctls for PHC timestamps with " Arthur Kiyanovski
2026-08-04 17:59 ` Maciek Machnikowski
2026-07-29 23:37 ` [PATCH v6 net-next 2/7] selftests/ptp: Extract print_system_timestamp helper in testptp Arthur Kiyanovski
2026-07-29 23:37 ` [PATCH v6 net-next 3/7] selftests/ptp: Add testptp support for attributes ioctls Arthur Kiyanovski
2026-07-29 23:37 ` [PATCH v6 net-next 4/7] ptp: ptp_vmclock: Implement " Arthur Kiyanovski
2026-08-04 12:37 ` Simon Horman [this message]
2026-07-29 23:37 ` [PATCH v6 net-next 5/7] net: ena: Update PHC admin interface for error bound support Arthur Kiyanovski
2026-07-29 23:37 ` [PATCH v6 net-next 6/7] net: ena: Add error bound to PHC communication layer Arthur Kiyanovski
2026-07-29 23:37 ` [PATCH v6 net-next 7/7] net: ena: Implement gettimexattrs64 callback for PTP attributes Arthur Kiyanovski
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=20260804123739.GM51943@horms.kernel.org \
--to=horms@kernel.org \
--cc=akiyano@amazon.com \
--cc=aliguori@amazon.com \
--cc=alisaidi@amazon.com \
--cc=amitbern@amazon.com \
--cc=andrew+netdev@lunn.ch \
--cc=andrew@lunn.ch \
--cc=benh@amazon.com \
--cc=bhelgaas@google.com \
--cc=cjubran@nvidia.com \
--cc=corbet@lwn.net \
--cc=darinzon@amazon.com \
--cc=davem@davemloft.net \
--cc=dwmw2@infradead.org \
--cc=dwmw@amazon.co.uk \
--cc=dwmw@amazon.com \
--cc=edumazet@google.com \
--cc=evgenys@amazon.com \
--cc=evostrov@amazon.com \
--cc=guwen@linux.alibaba.com \
--cc=kuba@kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=matua@amazon.com \
--cc=mlichvar@redhat.com \
--cc=msw@amazon.com \
--cc=nafea@amazon.com \
--cc=ndagan@amazon.com \
--cc=netanel@amazon.com \
--cc=netdev@vger.kernel.org \
--cc=ofirt@amazon.com \
--cc=pabeni@redhat.com \
--cc=richardcochran@gmail.com \
--cc=saeedb@amazon.com \
--cc=shuah@kernel.org \
--cc=skhan@linuxfoundation.org \
--cc=tglx@linutronix.de \
--cc=vadim.fedorenko@linux.dev \
--cc=xuanzhuo@linux.alibaba.com \
--cc=ysarna@amazon.com \
--cc=zorik@amazon.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox