Skip to content

Commit 3ddee7f

Browse files
wangbaolin719arndb
authored andcommitted
ALSA: Avoid using timespec for struct snd_pcm_status
The struct snd_pcm_status will use 'timespec' type variables to record timestamp, which is not year 2038 safe on 32bits system. Userspace will use SNDRV_PCM_IOCTL_STATUS and SNDRV_PCM_IOCTL_STATUS_EXT as commands to issue ioctl() to fill the 'snd_pcm_status' structure in userspace. The command number is always defined through _IOR/_IOW/IORW, so when userspace changes the definition of 'struct timespec' to use 64-bit types, the command number also changes. Thus in the kernel, we now need to define two versions of each such ioctl and corresponding ioctl commands to handle 32bit time_t and 64bit time_t in native mode: struct snd_pcm_status32 { ...... s32 trigger_tstamp_sec; s32 trigger_tstamp_nsec; ...... s32 audio_tstamp_sec; s32 audio_tstamp_nsec; ...... }; struct snd_pcm_status64 { ...... s32 trigger_tstamp_sec; s32 trigger_tstamp_nsec; ...... s32 audio_tstamp_sec; s32 audio_tstamp_nsec; ...... }; Moreover in compat file, we renamed or introduced new structures to handle 32bit/64bit time_t in compatible mode. The 'struct snd_pcm_status32' and snd_pcm_status_user32() are used to handle 32bit time_t in compat mode. 'struct compat_snd_pcm_status64' and snd_pcm_status_user_compat64() are used to handle 64bit time_t. The implicit padding before timespec is made explicit to avoid incompatible structure layout between 32-bit and 64-bit x86 due to the different alignment requirements, and the snd_pcm_status structure is now hidden from the kernel to avoid relying on the timespec definitio definitionn Finally we can replace SNDRV_PCM_IOCTL_STATUS and SNDRV_PCM_IOCTL_STATUS_EXT with new commands and introduce new functions to fill new 'struct snd_pcm_status64' instead of using unsafe 'struct snd_pcm_status'. Then in future, the new commands can be matched when userspace changes 'timespec' to 64bit type to make a size change of 'struct snd_pcm_status'. When glibc changes time_t to 64-bit, any recompiled program will issue ioctl commands that the kernel does not understand without this patch. Signed-off-by: Baolin Wang <baolin.wang@linaro.org> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
1 parent a4e7dd3 commit 3ddee7f

5 files changed

Lines changed: 198 additions & 126 deletions

File tree

‎include/sound/pcm.h‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct snd_pcm_hardware {
4444
size_t fifo_size; /* fifo size in bytes */
4545
};
4646

47+
struct snd_pcm_status64;
4748
struct snd_pcm_substream;
4849

4950
struct snd_pcm_audio_tstamp_config; /* definitions further down */
@@ -558,8 +559,8 @@ int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree);
558559
int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info);
559560
int snd_pcm_info_user(struct snd_pcm_substream *substream,
560561
struct snd_pcm_info __user *info);
561-
int snd_pcm_status(struct snd_pcm_substream *substream,
562-
struct snd_pcm_status *status);
562+
int snd_pcm_status64(struct snd_pcm_substream *substream,
563+
struct snd_pcm_status64 *status);
563564
int snd_pcm_start(struct snd_pcm_substream *substream);
564565
int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t status);
565566
int snd_pcm_drain_done(struct snd_pcm_substream *substream);
@@ -1422,4 +1423,55 @@ static inline u64 pcm_format_to_bits(snd_pcm_format_t pcm_format)
14221423
#define pcm_dbg(pcm, fmt, args...) \
14231424
dev_dbg((pcm)->card->dev, fmt, ##args)
14241425

1426+
struct snd_pcm_status64 {
1427+
snd_pcm_state_t state; /* stream state */
1428+
u8 rsvd[4];
1429+
s64 trigger_tstamp_sec; /* time when stream was started/stopped/paused */
1430+
s64 trigger_tstamp_nsec;
1431+
s64 tstamp_sec; /* reference timestamp */
1432+
s64 tstamp_nsec;
1433+
snd_pcm_uframes_t appl_ptr; /* appl ptr */
1434+
snd_pcm_uframes_t hw_ptr; /* hw ptr */
1435+
snd_pcm_sframes_t delay; /* current delay in frames */
1436+
snd_pcm_uframes_t avail; /* number of frames available */
1437+
snd_pcm_uframes_t avail_max; /* max frames available on hw since last status */
1438+
snd_pcm_uframes_t overrange; /* count of ADC (capture) overrange detections from last status */
1439+
snd_pcm_state_t suspended_state; /* suspended stream state */
1440+
__u32 audio_tstamp_data; /* needed for 64-bit alignment, used for configs/report to/from userspace */
1441+
s64 audio_tstamp_sec; /* sample counter, wall clock, PHC or on-demand sync'ed */
1442+
s64 audio_tstamp_nsec;
1443+
s64 driver_tstamp_sec; /* useful in case reference system tstamp is reported with delay */
1444+
s64 driver_tstamp_nsec;
1445+
__u32 audio_tstamp_accuracy; /* in ns units, only valid if indicated in audio_tstamp_data */
1446+
unsigned char reserved[52-4*sizeof(s64)]; /* must be filled with zero */
1447+
};
1448+
1449+
#define SNDRV_PCM_IOCTL_STATUS64 _IOR('A', 0x20, struct snd_pcm_status64)
1450+
#define SNDRV_PCM_IOCTL_STATUS_EXT64 _IOWR('A', 0x24, struct snd_pcm_status64)
1451+
1452+
struct snd_pcm_status32 {
1453+
s32 state; /* stream state */
1454+
s32 trigger_tstamp_sec; /* time when stream was started/stopped/paused */
1455+
s32 trigger_tstamp_nsec;
1456+
s32 tstamp_sec; /* reference timestamp */
1457+
s32 tstamp_nsec;
1458+
u32 appl_ptr; /* appl ptr */
1459+
u32 hw_ptr; /* hw ptr */
1460+
s32 delay; /* current delay in frames */
1461+
u32 avail; /* number of frames available */
1462+
u32 avail_max; /* max frames available on hw since last status */
1463+
u32 overrange; /* count of ADC (capture) overrange detections from last status */
1464+
s32 suspended_state; /* suspended stream state */
1465+
u32 audio_tstamp_data; /* needed for 64-bit alignment, used for configs/report to/from userspace */
1466+
s32 audio_tstamp_sec; /* sample counter, wall clock, PHC or on-demand sync'ed */
1467+
s32 audio_tstamp_nsec;
1468+
s32 driver_tstamp_sec; /* useful in case reference system tstamp is reported with delay */
1469+
s32 driver_tstamp_nsec;
1470+
u32 audio_tstamp_accuracy; /* in ns units, only valid if indicated in audio_tstamp_data */
1471+
unsigned char reserved[52-4*sizeof(s32)]; /* must be filled with zero */
1472+
};
1473+
1474+
#define SNDRV_PCM_IOCTL_STATUS32 _IOR('A', 0x20, struct snd_pcm_status32)
1475+
#define SNDRV_PCM_IOCTL_STATUS_EXT32 _IOWR('A', 0x24, struct snd_pcm_status32)
1476+
14251477
#endif /* __SOUND_PCM_H */

‎include/uapi/sound/asound.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,13 @@ enum {
456456
SNDRV_PCM_AUDIO_TSTAMP_TYPE_LAST = SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK_SYNCHRONIZED
457457
};
458458

459+
#ifndef __KERNEL__
460+
/* explicit padding avoids incompatibility between i386 and x86-64 */
461+
typedef struct { unsigned char pad[sizeof(time_t) - sizeof(int)] __time_pad;
462+
459463
struct snd_pcm_status {
460464
snd_pcm_state_t state; /* stream state */
465+
__time_pad pad1; /* align to timespec */
461466
struct timespec trigger_tstamp; /* time when stream was started/stopped/paused */
462467
struct timespec tstamp; /* reference timestamp */
463468
snd_pcm_uframes_t appl_ptr; /* appl ptr */
@@ -473,6 +478,7 @@ struct snd_pcm_status {
473478
__u32 audio_tstamp_accuracy; /* in ns units, only valid if indicated in audio_tstamp_data */
474479
unsigned char reserved[52-2*sizeof(struct timespec)]; /* must be filled with zero */
475480
};
481+
#endif
476482

477483
struct snd_pcm_mmap_status {
478484
snd_pcm_state_t state; /* RO: state - SNDRV_PCM_STATE_XXXX */

‎sound/core/pcm.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
443443
{
444444
struct snd_pcm_substream *substream = entry->private_data;
445445
struct snd_pcm_runtime *runtime;
446-
struct snd_pcm_status status;
446+
struct snd_pcm_status64 status;
447447
int err;
448448

449449
mutex_lock(&substream->pcm->open_mutex);
@@ -453,17 +453,17 @@ static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
453453
goto unlock;
454454
}
455455
memset(&status, 0, sizeof(status));
456-
err = snd_pcm_status(substream, &status);
456+
err = snd_pcm_status64(substream, &status);
457457
if (err < 0) {
458458
snd_iprintf(buffer, "error %d\n", err);
459459
goto unlock;
460460
}
461461
snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
462462
snd_iprintf(buffer, "owner_pid : %d\n", pid_vnr(substream->pid));
463-
snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
464-
status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
465-
snd_iprintf(buffer, "tstamp : %ld.%09ld\n",
466-
status.tstamp.tv_sec, status.tstamp.tv_nsec);
463+
snd_iprintf(buffer, "trigger_time: %lld.%09lld\n",
464+
status.trigger_tstamp_sec, status.trigger_tstamp_nsec);
465+
snd_iprintf(buffer, "tstamp : %lld.%09lld\n",
466+
status.tstamp_sec, status.tstamp_nsec);
467467
snd_iprintf(buffer, "delay : %ld\n", status.delay);
468468
snd_iprintf(buffer, "avail : %ld\n", status.avail);
469469
snd_iprintf(buffer, "avail_max : %ld\n", status.avail_max);

‎sound/core/pcm_compat.c‎

Lines changed: 53 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -168,73 +168,13 @@ static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
168168
snd_pcm_channel_info_user(s, p)
169169
#endif /* CONFIG_X86_X32 */
170170

171-
struct snd_pcm_status32 {
171+
struct compat_snd_pcm_status64 {
172172
s32 state;
173-
struct compat_timespec trigger_tstamp;
174-
struct compat_timespec tstamp;
175-
u32 appl_ptr;
176-
u32 hw_ptr;
177-
s32 delay;
178-
u32 avail;
179-
u32 avail_max;
180-
u32 overrange;
181-
s32 suspended_state;
182-
u32 audio_tstamp_data;
183-
struct compat_timespec audio_tstamp;
184-
struct compat_timespec driver_tstamp;
185-
u32 audio_tstamp_accuracy;
186-
unsigned char reserved[52-2*sizeof(struct compat_timespec)];
187-
} __attribute__((packed));
188-
189-
190-
static int snd_pcm_status_user_compat(struct snd_pcm_substream *substream,
191-
struct snd_pcm_status32 __user *src,
192-
bool ext)
193-
{
194-
struct snd_pcm_status status;
195-
int err;
196-
197-
memset(&status, 0, sizeof(status));
198-
/*
199-
* with extension, parameters are read/write,
200-
* get audio_tstamp_data from user,
201-
* ignore rest of status structure
202-
*/
203-
if (ext && get_user(status.audio_tstamp_data,
204-
(u32 __user *)(&src->audio_tstamp_data)))
205-
return -EFAULT;
206-
err = snd_pcm_status(substream, &status);
207-
if (err < 0)
208-
return err;
209-
210-
if (clear_user(src, sizeof(*src)))
211-
return -EFAULT;
212-
if (put_user(status.state, &src->state) ||
213-
compat_put_timespec(&status.trigger_tstamp, &src->trigger_tstamp) ||
214-
compat_put_timespec(&status.tstamp, &src->tstamp) ||
215-
put_user(status.appl_ptr, &src->appl_ptr) ||
216-
put_user(status.hw_ptr, &src->hw_ptr) ||
217-
put_user(status.delay, &src->delay) ||
218-
put_user(status.avail, &src->avail) ||
219-
put_user(status.avail_max, &src->avail_max) ||
220-
put_user(status.overrange, &src->overrange) ||
221-
put_user(status.suspended_state, &src->suspended_state) ||
222-
put_user(status.audio_tstamp_data, &src->audio_tstamp_data) ||
223-
compat_put_timespec(&status.audio_tstamp, &src->audio_tstamp) ||
224-
compat_put_timespec(&status.driver_tstamp, &src->driver_tstamp) ||
225-
put_user(status.audio_tstamp_accuracy, &src->audio_tstamp_accuracy))
226-
return -EFAULT;
227-
228-
return err;
229-
}
230-
231-
#ifdef CONFIG_X86_X32
232-
/* X32 ABI has 64bit timespec and 64bit alignment */
233-
struct snd_pcm_status_x32 {
234-
s32 state;
235-
u32 rsvd; /* alignment */
236-
struct timespec trigger_tstamp;
237-
struct timespec tstamp;
173+
u8 rsvd[4]; /* alignment */
174+
s64 trigger_tstamp_sec;
175+
s64 trigger_tstamp_nsec;
176+
s64 tstamp_sec;
177+
s64 tstamp_nsec;
238178
u32 appl_ptr;
239179
u32 hw_ptr;
240180
s32 delay;
@@ -243,22 +183,26 @@ struct snd_pcm_status_x32 {
243183
u32 overrange;
244184
s32 suspended_state;
245185
u32 audio_tstamp_data;
246-
struct timespec audio_tstamp;
247-
struct timespec driver_tstamp;
186+
s64 audio_tstamp_sec;
187+
s64 audio_tstamp_nsec;
188+
s64 driver_tstamp_sec;
189+
s64 driver_tstamp_nsec;
248190
u32 audio_tstamp_accuracy;
249-
unsigned char reserved[52-2*sizeof(struct timespec)];
191+
unsigned char reserved[52-4*sizeof(s64)];
250192
} __packed;
251193

252194
#define put_timespec(src, dst) copy_to_user(dst, src, sizeof(*dst))
253195

254-
static int snd_pcm_status_user_x32(struct snd_pcm_substream *substream,
255-
struct snd_pcm_status_x32 __user *src,
256-
bool ext)
196+
static int snd_pcm_status_user_compat64(struct snd_pcm_substream *substream,
197+
struct compat_snd_pcm_status64 __user *src,
198+
bool ext)
257199
{
258-
struct snd_pcm_status status;
200+
struct snd_pcm_status64 status;
201+
struct compat_snd_pcm_status64 compat_status64;
259202
int err;
260203

261204
memset(&status, 0, sizeof(status));
205+
memset(&compat_status64, 0, sizeof(compat_status64));
262206
/*
263207
* with extension, parameters are read/write,
264208
* get audio_tstamp_data from user,
@@ -267,31 +211,39 @@ static int snd_pcm_status_user_x32(struct snd_pcm_substream *substream,
267211
if (ext && get_user(status.audio_tstamp_data,
268212
(u32 __user *)(&src->audio_tstamp_data)))
269213
return -EFAULT;
270-
err = snd_pcm_status(substream, &status);
214+
err = snd_pcm_status64(substream, &status);
271215
if (err < 0)
272216
return err;
273217

274218
if (clear_user(src, sizeof(*src)))
275219
return -EFAULT;
276-
if (put_user(status.state, &src->state) ||
277-
put_timespec(&status.trigger_tstamp, &src->trigger_tstamp) ||
278-
put_timespec(&status.tstamp, &src->tstamp) ||
279-
put_user(status.appl_ptr, &src->appl_ptr) ||
280-
put_user(status.hw_ptr, &src->hw_ptr) ||
281-
put_user(status.delay, &src->delay) ||
282-
put_user(status.avail, &src->avail) ||
283-
put_user(status.avail_max, &src->avail_max) ||
284-
put_user(status.overrange, &src->overrange) ||
285-
put_user(status.suspended_state, &src->suspended_state) ||
286-
put_user(status.audio_tstamp_data, &src->audio_tstamp_data) ||
287-
put_timespec(&status.audio_tstamp, &src->audio_tstamp) ||
288-
put_timespec(&status.driver_tstamp, &src->driver_tstamp) ||
289-
put_user(status.audio_tstamp_accuracy, &src->audio_tstamp_accuracy))
220+
221+
compat_status64 = (struct compat_snd_pcm_status64) {
222+
.state = status.state,
223+
.trigger_tstamp_sec = status.trigger_tstamp_sec,
224+
.trigger_tstamp_nsec = status.trigger_tstamp_nsec,
225+
.tstamp_sec = status.tstamp_sec,
226+
.tstamp_nsec = status.tstamp_nsec,
227+
.appl_ptr = status.appl_ptr,
228+
.hw_ptr = status.hw_ptr,
229+
.delay = status.delay,
230+
.avail = status.avail,
231+
.avail_max = status.avail_max,
232+
.overrange = status.overrange,
233+
.suspended_state = status.suspended_state,
234+
.audio_tstamp_data = status.audio_tstamp_data,
235+
.audio_tstamp_sec = status.audio_tstamp_sec,
236+
.audio_tstamp_nsec = status.audio_tstamp_nsec,
237+
.driver_tstamp_sec = status.audio_tstamp_sec,
238+
.driver_tstamp_nsec = status.audio_tstamp_nsec,
239+
.audio_tstamp_accuracy = status.audio_tstamp_accuracy,
240+
};
241+
242+
if (copy_to_user(src, &compat_status64, sizeof(compat_status64)))
290243
return -EFAULT;
291244

292245
return err;
293246
}
294-
#endif /* CONFIG_X86_X32 */
295247

296248
/* both for HW_PARAMS and HW_REFINE */
297249
static int snd_pcm_ioctl_hw_params_compat(struct snd_pcm_substream *substream,
@@ -616,8 +568,8 @@ enum {
616568
SNDRV_PCM_IOCTL_HW_REFINE32 = _IOWR('A', 0x10, struct snd_pcm_hw_params32),
617569
SNDRV_PCM_IOCTL_HW_PARAMS32 = _IOWR('A', 0x11, struct snd_pcm_hw_params32),
618570
SNDRV_PCM_IOCTL_SW_PARAMS32 = _IOWR('A', 0x13, struct snd_pcm_sw_params32),
619-
SNDRV_PCM_IOCTL_STATUS32 = _IOR('A', 0x20, struct snd_pcm_status32),
620-
SNDRV_PCM_IOCTL_STATUS_EXT32 = _IOWR('A', 0x24, struct snd_pcm_status32),
571+
SNDRV_PCM_IOCTL_STATUS_COMPAT32 = _IOR('A', 0x20, struct snd_pcm_status32),
572+
SNDRV_PCM_IOCTL_STATUS_EXT_COMPAT32 = _IOWR('A', 0x24, struct snd_pcm_status32),
621573
SNDRV_PCM_IOCTL_DELAY32 = _IOR('A', 0x21, s32),
622574
SNDRV_PCM_IOCTL_CHANNEL_INFO32 = _IOR('A', 0x32, struct snd_pcm_channel_info32),
623575
SNDRV_PCM_IOCTL_REWIND32 = _IOW('A', 0x46, u32),
@@ -627,10 +579,10 @@ enum {
627579
SNDRV_PCM_IOCTL_WRITEN_FRAMES32 = _IOW('A', 0x52, struct snd_xfern32),
628580
SNDRV_PCM_IOCTL_READN_FRAMES32 = _IOR('A', 0x53, struct snd_xfern32),
629581
SNDRV_PCM_IOCTL_SYNC_PTR32 = _IOWR('A', 0x23, struct snd_pcm_sync_ptr32),
582+
SNDRV_PCM_IOCTL_STATUS_COMPAT64 = _IOR('A', 0x20, struct compat_snd_pcm_status64),
583+
SNDRV_PCM_IOCTL_STATUS_EXT_COMPAT64 = _IOWR('A', 0x24, struct compat_snd_pcm_status64),
630584
#ifdef CONFIG_X86_X32
631585
SNDRV_PCM_IOCTL_CHANNEL_INFO_X32 = _IOR('A', 0x32, struct snd_pcm_channel_info),
632-
SNDRV_PCM_IOCTL_STATUS_X32 = _IOR('A', 0x20, struct snd_pcm_status_x32),
633-
SNDRV_PCM_IOCTL_STATUS_EXT_X32 = _IOWR('A', 0x24, struct snd_pcm_status_x32),
634586
SNDRV_PCM_IOCTL_SYNC_PTR_X32 = _IOWR('A', 0x23, struct snd_pcm_sync_ptr_x32),
635587
#endif /* CONFIG_X86_X32 */
636588
};
@@ -680,10 +632,10 @@ static long snd_pcm_ioctl_compat(struct file *file, unsigned int cmd, unsigned l
680632
return snd_pcm_ioctl_hw_params_compat(substream, 0, argp);
681633
case SNDRV_PCM_IOCTL_SW_PARAMS32:
682634
return snd_pcm_ioctl_sw_params_compat(substream, argp);
683-
case SNDRV_PCM_IOCTL_STATUS32:
684-
return snd_pcm_status_user_compat(substream, argp, false);
685-
case SNDRV_PCM_IOCTL_STATUS_EXT32:
686-
return snd_pcm_status_user_compat(substream, argp, true);
635+
case SNDRV_PCM_IOCTL_STATUS_COMPAT32:
636+
return snd_pcm_status_user32(substream, argp, false);
637+
case SNDRV_PCM_IOCTL_STATUS_EXT_COMPAT32:
638+
return snd_pcm_status_user32(substream, argp, true);
687639
case SNDRV_PCM_IOCTL_SYNC_PTR32:
688640
return snd_pcm_ioctl_sync_ptr_compat(substream, argp);
689641
case SNDRV_PCM_IOCTL_CHANNEL_INFO32:
@@ -702,11 +654,11 @@ static long snd_pcm_ioctl_compat(struct file *file, unsigned int cmd, unsigned l
702654
return snd_pcm_ioctl_rewind_compat(substream, argp);
703655
case SNDRV_PCM_IOCTL_FORWARD32:
704656
return snd_pcm_ioctl_forward_compat(substream, argp);
657+
case SNDRV_PCM_IOCTL_STATUS_COMPAT64:
658+
return snd_pcm_status_user_compat64(substream, argp, false);
659+
case SNDRV_PCM_IOCTL_STATUS_EXT_COMPAT64:
660+
return snd_pcm_status_user_compat64(substream, argp, true);
705661
#ifdef CONFIG_X86_X32
706-
case SNDRV_PCM_IOCTL_STATUS_X32:
707-
return snd_pcm_status_user_x32(substream, argp, false);
708-
case SNDRV_PCM_IOCTL_STATUS_EXT_X32:
709-
return snd_pcm_status_user_x32(substream, argp, true);
710662
case SNDRV_PCM_IOCTL_SYNC_PTR_X32:
711663
return snd_pcm_ioctl_sync_ptr_x32(substream, argp);
712664
case SNDRV_PCM_IOCTL_CHANNEL_INFO_X32:

0 commit comments

Comments
 (0)