Commit 475b92f9 authored by Jakub Kicinski's avatar Jakub Kicinski Committed by David S. Miller

ptp: improve max_adj check against unreasonable values

Scaled PPM conversion to PPB may (on 64bit systems) result
in a value larger than s32 can hold (freq/scaled_ppm is a long).
This means the kernel will not correctly reject unreasonably
high ->freq values (e.g. > 4294967295ppb, 281474976645 scaled PPM).

The conversion is equivalent to a division by ~66 (65.536),
so the value of ppb is always smaller than ppm, but not small
enough to assume narrowing the type from long -> s32 is okay.

Note that reasonable user space (e.g. ptp4l) will not use such
high values, anyway, 4289046510ppb ~= 4.3x, so the fix is
somewhat pedantic.

Fixes: d39a7435 ("ptp: validate the requested frequency adjustment.")
Fixes: d94ba80e ("ptp: Added a brand new class driver for ptp clocks.")
Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
Acked-by: default avatarRichard Cochran <richardcochran@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 2214fb53
...@@ -63,7 +63,7 @@ static void enqueue_external_timestamp(struct timestamp_event_queue *queue, ...@@ -63,7 +63,7 @@ static void enqueue_external_timestamp(struct timestamp_event_queue *queue,
spin_unlock_irqrestore(&queue->lock, flags); spin_unlock_irqrestore(&queue->lock, flags);
} }
s32 scaled_ppm_to_ppb(long ppm) long scaled_ppm_to_ppb(long ppm)
{ {
/* /*
* The 'freq' field in the 'struct timex' is in parts per * The 'freq' field in the 'struct timex' is in parts per
...@@ -80,7 +80,7 @@ s32 scaled_ppm_to_ppb(long ppm) ...@@ -80,7 +80,7 @@ s32 scaled_ppm_to_ppb(long ppm)
s64 ppb = 1 + ppm; s64 ppb = 1 + ppm;
ppb *= 125; ppb *= 125;
ppb >>= 13; ppb >>= 13;
return (s32) ppb; return (long) ppb;
} }
EXPORT_SYMBOL(scaled_ppm_to_ppb); EXPORT_SYMBOL(scaled_ppm_to_ppb);
...@@ -138,7 +138,7 @@ static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx) ...@@ -138,7 +138,7 @@ static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx)
delta = ktime_to_ns(kt); delta = ktime_to_ns(kt);
err = ops->adjtime(ops, delta); err = ops->adjtime(ops, delta);
} else if (tx->modes & ADJ_FREQUENCY) { } else if (tx->modes & ADJ_FREQUENCY) {
s32 ppb = scaled_ppm_to_ppb(tx->freq); long ppb = scaled_ppm_to_ppb(tx->freq);
if (ppb > ops->max_adj || ppb < -ops->max_adj) if (ppb > ops->max_adj || ppb < -ops->max_adj)
return -ERANGE; return -ERANGE;
if (ops->adjfine) if (ops->adjfine)
......
...@@ -235,7 +235,7 @@ extern int ptp_clock_index(struct ptp_clock *ptp); ...@@ -235,7 +235,7 @@ extern int ptp_clock_index(struct ptp_clock *ptp);
* @ppm: Parts per million, but with a 16 bit binary fractional field * @ppm: Parts per million, but with a 16 bit binary fractional field
*/ */
extern s32 scaled_ppm_to_ppb(long ppm); extern long scaled_ppm_to_ppb(long ppm);
/** /**
* ptp_find_pin() - obtain the pin index of a given auxiliary function * ptp_find_pin() - obtain the pin index of a given auxiliary function
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment