Skip to content

Commit 3b9ab37

Browse files
Bamvor Jian Zhanggregkh
authored andcommitted
ppdev: convert to y2038 safe
The y2038 issue for ppdev is changes of timeval in the ioctl (PPSETTIME and PPGETTIME). The size of struct timeval changes from 8bytes to 16bytes due to the changes of time_t. It lead to the changes of the command of ioctl, e.g. for PPGETTIME, We have: on 32-bit (old): 0x80087095 on 32-bit (new): 0x80107095 on 64-bit : 0x80107095 This patch define these two ioctl commands to support the 32bit and 64bit time_t application at the same time. And, introduce pp_set_timeout to remove some duplicated code. Signed-off-by: Bamvor Jian Zhang <bamvor.zhangjian@linaro.org> Reviewed-by: Arnd Bergmann <arnd@arndb.de> Tested-by: Sudip Mukherjee <sudipm.mukherjee@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 468623b commit 3b9ab37

1 file changed

Lines changed: 55 additions & 20 deletions

File tree

‎drivers/char/ppdev.c‎

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ struct pp_struct {
9898
#define ROUND_UP(x,y) (((x)+(y)-1)/(y))
9999

100100
static DEFINE_MUTEX(pp_do_mutex);
101+
102+
/* define fixed sized ioctl cmd for y2038 migration */
103+
#define PPGETTIME32 _IOR(PP_IOCTL, 0x95, s32[2])
104+
#define PPSETTIME32 _IOW(PP_IOCTL, 0x96, s32[2])
105+
#define PPGETTIME64 _IOR(PP_IOCTL, 0x95, s64[2])
106+
#define PPSETTIME64 _IOW(PP_IOCTL, 0x96, s64[2])
107+
101108
static inline void pp_enable_irq (struct pp_struct *pp)
102109
{
103110
struct parport *port = pp->pdev->port;
@@ -322,6 +329,22 @@ static enum ieee1284_phase init_phase (int mode)
322329
return IEEE1284_PH_FWD_IDLE;
323330
}
324331

332+
static int pp_set_timeout(struct pardevice *pdev, long tv_sec, int tv_usec)
333+
{
334+
long to_jiffies;
335+
336+
if ((tv_sec < 0) || (tv_usec < 0))
337+
return -EINVAL;
338+
339+
to_jiffies = usecs_to_jiffies(tv_usec);
340+
to_jiffies += tv_sec * HZ;
341+
if (to_jiffies <= 0)
342+
return -EINVAL;
343+
344+
pdev->timeout = to_jiffies;
345+
return 0;
346+
}
347+
325348
static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
326349
{
327350
unsigned int minor = iminor(file_inode(file));
@@ -495,9 +518,10 @@ static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
495518
unsigned char reg;
496519
unsigned char mask;
497520
int mode;
521+
s32 time32[2];
522+
s64 time64[2];
523+
struct timespec64 ts;
498524
int ret;
499-
struct timeval par_timeout;
500-
long to_jiffies;
501525

502526
case PPRSTATUS:
503527
reg = parport_read_status (port);
@@ -592,29 +616,40 @@ static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
592616
atomic_sub (ret, &pp->irqc);
593617
return 0;
594618

595-
case PPSETTIME:
596-
if (copy_from_user (&par_timeout, argp, sizeof(struct timeval))) {
619+
case PPSETTIME32:
620+
if (copy_from_user(time32, argp, sizeof(time32)))
597621
return -EFAULT;
598-
}
599-
/* Convert to jiffies, place in pp->pdev->timeout */
600-
if ((par_timeout.tv_sec < 0) || (par_timeout.tv_usec < 0)) {
601-
return -EINVAL;
602-
}
603-
to_jiffies = ROUND_UP(par_timeout.tv_usec, 1000000/HZ);
604-
to_jiffies += par_timeout.tv_sec * (long)HZ;
605-
if (to_jiffies <= 0) {
622+
623+
return pp_set_timeout(pp->pdev, time32[0], time32[1]);
624+
625+
case PPSETTIME64:
626+
if (copy_from_user(time64, argp, sizeof(time64)))
627+
return -EFAULT;
628+
629+
return pp_set_timeout(pp->pdev, time64[0], time64[1]);
630+
631+
case PPGETTIME32:
632+
jiffies_to_timespec64(pp->pdev->timeout, &ts);
633+
time32[0] = ts.tv_sec;
634+
time32[1] = ts.tv_nsec / NSEC_PER_USEC;
635+
if ((time32[0] < 0) || (time32[1] < 0))
606636
return -EINVAL;
607-
}
608-
pp->pdev->timeout = to_jiffies;
637+
638+
if (copy_to_user(argp, time32, sizeof(time32)))
639+
return -EFAULT;
640+
609641
return 0;
610642

611-
case PPGETTIME:
612-
to_jiffies = pp->pdev->timeout;
613-
memset(&par_timeout, 0, sizeof(par_timeout));
614-
par_timeout.tv_sec = to_jiffies / HZ;
615-
par_timeout.tv_usec = (to_jiffies % (long)HZ) * (1000000/HZ);
616-
if (copy_to_user (argp, &par_timeout, sizeof(struct timeval)))
643+
case PPGETTIME64:
644+
jiffies_to_timespec64(pp->pdev->timeout, &ts);
645+
time64[0] = ts.tv_sec;
646+
time64[1] = ts.tv_nsec / NSEC_PER_USEC;
647+
if ((time64[0] < 0) || (time64[1] < 0))
648+
return -EINVAL;
649+
650+
if (copy_to_user(argp, time64, sizeof(time64)))
617651
return -EFAULT;
652+
618653
return 0;
619654

620655
default:

0 commit comments

Comments
 (0)