Skip to content

Commit 475b92f

Browse files
kuba-moodavem330
authored andcommitted
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: d39a743 ("ptp: validate the requested frequency adjustment.") Fixes: d94ba80 ("ptp: Added a brand new class driver for ptp clocks.") Signed-off-by: Jakub Kicinski <[email protected]> Acked-by: Richard Cochran <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 2214fb5 commit 475b92f

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

drivers/ptp/ptp_clock.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static void enqueue_external_timestamp(struct timestamp_event_queue *queue,
6363
spin_unlock_irqrestore(&queue->lock, flags);
6464
}
6565

66-
s32 scaled_ppm_to_ppb(long ppm)
66+
long scaled_ppm_to_ppb(long ppm)
6767
{
6868
/*
6969
* The 'freq' field in the 'struct timex' is in parts per
@@ -80,7 +80,7 @@ s32 scaled_ppm_to_ppb(long ppm)
8080
s64 ppb = 1 + ppm;
8181
ppb *= 125;
8282
ppb >>= 13;
83-
return (s32) ppb;
83+
return (long) ppb;
8484
}
8585
EXPORT_SYMBOL(scaled_ppm_to_ppb);
8686

@@ -138,7 +138,7 @@ static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx)
138138
delta = ktime_to_ns(kt);
139139
err = ops->adjtime(ops, delta);
140140
} else if (tx->modes & ADJ_FREQUENCY) {
141-
s32 ppb = scaled_ppm_to_ppb(tx->freq);
141+
long ppb = scaled_ppm_to_ppb(tx->freq);
142142
if (ppb > ops->max_adj || ppb < -ops->max_adj)
143143
return -ERANGE;
144144
if (ops->adjfine)

include/linux/ptp_clock_kernel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ extern int ptp_clock_index(struct ptp_clock *ptp);
235235
* @ppm: Parts per million, but with a 16 bit binary fractional field
236236
*/
237237

238-
extern s32 scaled_ppm_to_ppb(long ppm);
238+
extern long scaled_ppm_to_ppb(long ppm);
239239

240240
/**
241241
* ptp_find_pin() - obtain the pin index of a given auxiliary function

0 commit comments

Comments
 (0)