From patchwork Thu Nov 2 11:06:53 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\(Exiting\) Baolin Wang" X-Patchwork-Id: 117777 Delivered-To: patch@linaro.org Received: by 10.80.245.45 with SMTP id t42csp1917906edm; Thu, 2 Nov 2017 04:08:21 -0700 (PDT) X-Google-Smtp-Source: ABhQp+RqrtEUJlhJwgwXrveRgJE7NRFTu6M8BmaFbk7xRhPyVxj0fSe9UV3KM1/txKpLwz8kcy66 X-Received: by 10.98.17.156 with SMTP id 28mr3347619pfr.80.1509620901661; Thu, 02 Nov 2017 04:08:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1509620901; cv=none; d=google.com; s=arc-20160816; b=zTk3POg/6JxAyqZUktQANN5HmOfa6rsBdoM2NPB/fjP9eC8JMZODiQTwXooTTtZ0mT Mo962jokmzwHB1yIjkxtry/Peiiz8zHF7t9fTcn+7BXvhnF9G8YDmfQZzzBBABeqrpic oBZDoffUj48qmguqS2GUKjJ7kyeHgEwhYWkIEAbMXwFuwwWuf84mqlNwQE5jjNxTfyO4 3+dxGDK4jD533skFpZKah7aGQWT1vVY0MR09ZWaElU1B5QKO+9lQ7dkJ+RTzk3SWqjDo 0m7uSAFxoOSFgSXZmKiLEJ2NShbgOxwIiZR9wZsEJUjw3JzVtR/e0VFxOhS8ApwEe3S9 QgCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=UtK0B0foQNsfHd+nXlPZOq39fUsWHpl7m3knx0LoNJ0=; b=Q4ADezeSQRGEYD4MwNKedQDv7j+IABFbV5vdYNpiWb3fdrLgHwj6K9aTk/AyFWAlaZ oAGAs4f0GAntqwyck4iViP9vhF4HUg0wudzRzpAtfY3d+HC4m/YZ3Rywgvg42CQz26e6 ITGpVlvLdwPO8flK0nIz9cOydZBk1OVSVnWKSrW8pV1caUAjVzTnPVhmTnUCPtZYD6ZH FhQxfalblvf0e6tNCt0bnlGBtNLDgT8FQw7RLOd2u0BLJ4wW5v+lydsNbYo3lGsd3iLp SbZn5g1ejeaUP6iTDGKv3Fa92rQ+Ck6vIJ/vcuejuKJUU0cayB/QdlZ23r4XKMwxR8BB TVcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GYHZsS2Y; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p27si3434851pgd.44.2017.11.02.04.08.21; Thu, 02 Nov 2017 04:08:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GYHZsS2Y; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755688AbdKBLIT (ORCPT + 26 others); Thu, 2 Nov 2017 07:08:19 -0400 Received: from mail-pf0-f195.google.com ([209.85.192.195]:57258 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755655AbdKBLIQ (ORCPT ); Thu, 2 Nov 2017 07:08:16 -0400 Received: by mail-pf0-f195.google.com with SMTP id b85so4319357pfj.13 for ; Thu, 02 Nov 2017 04:08:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=UtK0B0foQNsfHd+nXlPZOq39fUsWHpl7m3knx0LoNJ0=; b=GYHZsS2YLfdjs/dcXj80n9xYbyPzTl+HLaqgSqt8/gSzdpD0dr0HsD3UoLCzqFbq5e fFD3kPv8CpNBYvQTn9Oc2GIXZ7AbYGEp/9JO7Isbp5kW+nOYiotqSEhG/WYdRqyPxKi+ XJXd9z5MbJ0MC6FNR15fmBqvyNZBTItoqBKww= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=UtK0B0foQNsfHd+nXlPZOq39fUsWHpl7m3knx0LoNJ0=; b=mvxexutgA9ZJecJp6gKUyj7rWYvouJQ04fJkVWJ9H4UkjTpDoJFXdi1GaSwqkUMCan 5EyuBJ1PWl1H/9aogTzBU7o6ab5uNlGwwAHPiiW4yl3FZyqBe8MuI5GYex+p1OgdqzvR h1FjVhB+2dNL7DlNZPOAueZfAzKVErxl6j5qLwPbN2Cph4AhN122yusneA2xJm2Ptbif K0jYo2+2Ayb+ccgLH7Siv62cTXXqNSjZ0SYtyWcVmCSLcQVD3Q2qxXbZrbuFaxHHypWj VWaWdLE0eimVax0fh/ddNo4oYAXu2lQzJuum32ap4YoqJjVaWMYYVzMMaqlfgP+tFaoU 8Qlw== X-Gm-Message-State: AMCzsaX9Hq9H7fFCaEQC+9c3tgVbNGgc0UAkMX6wx9zospe8d/XqFsly wV8McqWDUFnMmQzmQV7wR7kjNQ== X-Received: by 10.101.88.14 with SMTP id g14mr3228720pgr.412.1509620896363; Thu, 02 Nov 2017 04:08:16 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id g13sm5473772pfm.130.2017.11.02.04.08.11 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 02 Nov 2017 04:08:15 -0700 (PDT) From: Baolin Wang To: perex@perex.cz, tiwai@suse.com, arnd@arndb.de Cc: lgirdwood@gmail.com, broonie@kernel.org, o-takashi@sakamocchi.jp, mingo@kernel.org, elfring@users.sourceforge.net, dan.carpenter@oracle.com, jeeja.kp@intel.com, vinod.koul@intel.com, guneshwor.o.singh@intel.com, subhransu.s.prusty@intel.com, bhumirks@gmail.com, gudishax.kranthikumar@intel.com, naveen.m@intel.com, hardik.t.shah@intel.com, arvind.yadav.cs@gmail.com, fabf@skynet.be, alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org, baolin.wang@linaro.org Subject: [RFC PATCH v2 3/7] sound: core: Avoid using timespec for struct snd_rawmidi_status Date: Thu, 2 Nov 2017 19:06:53 +0800 Message-Id: <4aa4f178fed669b1d2b26965f81e934ae456322a.1509612176.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The struct snd_rawmidi_status will use 'timespec' type variables to record timestamp, which is not year 2038 safe on 32bits system. Thus we introduced 'struct snd_rawmidi_status32' and 'struct snd_rawmidi_status64' to handle 32bit time_t and 64bit time_t in native mode, which replace timespec with s64 type. In compat mode, we renamed or introduced new structures to handle 32bit/64bit time_t in compatible mode. 'struct compat_snd_rawmidi_status32' and snd_rawmidi_ioctl_status_compat() are used to handle 32bit time_t in compat mode. 'struct compat_snd_rawmidi_status64' and snd_rawmidi_ioctl_status_compat64() are used to handle 64bit time_t with 64bit alignment. 'struct compat_snd_rawmidi_status64_x86_32' and snd_rawmidi_ioctl_status_compat64_x86_32() are used to handle 64bit time_t with 32bit alignment. 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 --- sound/core/rawmidi.c | 81 ++++++++++++++++++++++++++--- sound/core/rawmidi_compat.c | 121 +++++++++++++++++++++++++++++++++---------- 2 files changed, 168 insertions(+), 34 deletions(-) -- 1.7.9.5 diff --git a/sound/core/rawmidi.c b/sound/core/rawmidi.c index b3b353d..af79009 100644 --- a/sound/core/rawmidi.c +++ b/sound/core/rawmidi.c @@ -63,6 +63,30 @@ #define rmidi_dbg(rmidi, fmt, args...) \ dev_dbg(&(rmidi)->dev, fmt, ##args) +#if __BITS_PER_LONG == 32 +struct snd_rawmidi_status32 { + int stream; + s32 tstamp_sec; /* Timestamp */ + s32 tstamp_nsec; + size_t avail; /* available bytes */ + size_t xruns; /* count of overruns since last status (in bytes) */ + unsigned char reserved[16]; /* reserved for future use */ +}; + +#define SNDRV_RAWMIDI_IOCTL_STATUS32 _IOWR('W', 0x20, struct snd_rawmidi_status32) +#endif + +struct snd_rawmidi_status64 { + int stream; + s64 tstamp_sec; /* Timestamp */ + s64 tstamp_nsec; + size_t avail; /* available bytes */ + size_t xruns; /* count of overruns since last status (in bytes) */ + unsigned char reserved[16]; /* reserved for future use */ +}; + +#define SNDRV_RAWMIDI_IOCTL_STATUS64 _IOWR('W', 0x20, struct snd_rawmidi_status64) + static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device) { struct snd_rawmidi *rawmidi; @@ -680,7 +704,7 @@ int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream, EXPORT_SYMBOL(snd_rawmidi_input_params); static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream, - struct snd_rawmidi_status * status) + struct snd_rawmidi_status64 * status) { struct snd_rawmidi_runtime *runtime = substream->runtime; @@ -693,7 +717,7 @@ static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream, } static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream, - struct snd_rawmidi_status * status) + struct snd_rawmidi_status64 * status) { struct snd_rawmidi_runtime *runtime = substream->runtime; @@ -751,11 +775,55 @@ static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long return -EINVAL; } } - case SNDRV_RAWMIDI_IOCTL_STATUS: +#if __BITS_PER_LONG == 32 + case SNDRV_RAWMIDI_IOCTL_STATUS32: + { + int err = 0; + struct snd_rawmidi_status32 __user *status = argp; + struct snd_rawmidi_status32 status32; + struct snd_rawmidi_status64 status64; + + if (copy_from_user(&status32, argp, + sizeof(struct snd_rawmidi_status32))) + return -EFAULT; + switch (status32.stream) { + case SNDRV_RAWMIDI_STREAM_OUTPUT: + if (rfile->output == NULL) + return -EINVAL; + err = snd_rawmidi_output_status(rfile->output, &status64); + break; + case SNDRV_RAWMIDI_STREAM_INPUT: + if (rfile->input == NULL) + return -EINVAL; + err = snd_rawmidi_input_status(rfile->input, &status64); + break; + default: + return -EINVAL; + } + if (err < 0) + return err; + + status32 = (struct snd_rawmidi_status32) { + .stream = status64.stream, + .tstamp_sec = status64.tstamp_sec, + .tstamp_nsec = status64.tstamp_nsec, + .avail = status64.avail, + .xruns = status64.xruns, + }; + + if (copy_to_user(status, &status32, sizeof(*status))) + return -EFAULT; + + return 0; + } +#endif + case SNDRV_RAWMIDI_IOCTL_STATUS64: { int err = 0; - struct snd_rawmidi_status status; - if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status))) + struct snd_rawmidi_status64 status; + + if (copy_from_user(&status, argp, + sizeof(struct snd_rawmidi_status64))) return -EFAULT; switch (status.stream) { case SNDRV_RAWMIDI_STREAM_OUTPUT: @@ -773,7 +841,8 @@ static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long } if (err < 0) return err; - if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status))) + if (copy_to_user(argp, &status, + sizeof(struct snd_rawmidi_status64))) return -EFAULT; return 0; } diff --git a/sound/core/rawmidi_compat.c b/sound/core/rawmidi_compat.c index f69764d..5b5b5ea 100644 --- a/sound/core/rawmidi_compat.c +++ b/sound/core/rawmidi_compat.c @@ -53,19 +53,21 @@ static int snd_rawmidi_ioctl_params_compat(struct snd_rawmidi_file *rfile, return -EINVAL; } -struct snd_rawmidi_status32 { +struct compat_snd_rawmidi_status32 { s32 stream; - struct compat_timespec tstamp; + s32 tstamp_sec; + s32 tstamp_nsec; u32 avail; u32 xruns; unsigned char reserved[16]; } __attribute__((packed)); static int snd_rawmidi_ioctl_status_compat(struct snd_rawmidi_file *rfile, - struct snd_rawmidi_status32 __user *src) + struct compat_snd_rawmidi_status32 __user *src) { int err; - struct snd_rawmidi_status status; + struct snd_rawmidi_status64 status; + struct compat_snd_rawmidi_status32 compat_status; if (rfile->output == NULL) return -EINVAL; @@ -85,32 +87,86 @@ static int snd_rawmidi_ioctl_status_compat(struct snd_rawmidi_file *rfile, if (err < 0) return err; - if (compat_put_timespec(&status.tstamp, &src->tstamp) || - put_user(status.avail, &src->avail) || - put_user(status.xruns, &src->xruns)) + compat_status = (struct compat_snd_rawmidi_status32) { + .stream = status.stream, + .tstamp_sec = status.tstamp_sec, + .tstamp_nsec = status.tstamp_nsec, + .avail = status.avail, + .xruns = status.xruns, + }; + + if (copy_to_user(src, &compat_status, sizeof(*src))) return -EFAULT; return 0; } -#ifdef CONFIG_X86_X32 -/* X32 ABI has 64bit timespec and 64bit alignment */ -struct snd_rawmidi_status_x32 { +struct compat_snd_rawmidi_status64 { s32 stream; u32 rsvd; /* alignment */ - struct timespec tstamp; + s64 tstamp_sec; + s64 tstamp_nsec; u32 avail; u32 xruns; unsigned char reserved[16]; } __attribute__((packed)); -#define put_timespec(src, dst) copy_to_user(dst, src, sizeof(*dst)) +static int snd_rawmidi_ioctl_status_compat64(struct snd_rawmidi_file *rfile, + struct compat_snd_rawmidi_status64 __user *src) +{ + int err; + struct snd_rawmidi_status64 status; + struct compat_snd_rawmidi_status64 compat_status; + + if (rfile->output == NULL) + return -EINVAL; + if (get_user(status.stream, &src->stream)) + return -EFAULT; + + switch (status.stream) { + case SNDRV_RAWMIDI_STREAM_OUTPUT: + err = snd_rawmidi_output_status(rfile->output, &status); + break; + case SNDRV_RAWMIDI_STREAM_INPUT: + err = snd_rawmidi_input_status(rfile->input, &status); + break; + default: + return -EINVAL; + } + if (err < 0) + return err; + + compat_status = (struct compat_snd_rawmidi_status64) { + .stream = status.stream, + .tstamp_sec = status.tstamp_sec, + .tstamp_nsec = status.tstamp_nsec, + .avail = status.avail, + .xruns = status.xruns, + }; -static int snd_rawmidi_ioctl_status_x32(struct snd_rawmidi_file *rfile, - struct snd_rawmidi_status_x32 __user *src) + if (copy_to_user(src, &compat_status, sizeof(*src))) + return -EFAULT; + + return 0; +} + +#ifdef IA32_EMULATION +struct compat_snd_rawmidi_status64_x86_32 { + s32 stream; + s64 tstamp_sec; + s64 tstamp_nsec; + u32 avail; + u32 xruns; + unsigned char reserved[16]; +} __attribute__((packed)); + +static int +snd_rawmidi_ioctl_status_compat64_x86_32(struct snd_rawmidi_file *rfile, + struct compat_snd_rawmidi_status64_x86_32 __user *src) { int err; - struct snd_rawmidi_status status; + struct snd_rawmidi_status64 status; + struct compat_snd_rawmidi_status64_x86_32 compat_status; if (rfile->output == NULL) return -EINVAL; @@ -130,21 +186,28 @@ static int snd_rawmidi_ioctl_status_x32(struct snd_rawmidi_file *rfile, if (err < 0) return err; - if (put_timespec(&status.tstamp, &src->tstamp) || - put_user(status.avail, &src->avail) || - put_user(status.xruns, &src->xruns)) + compat_status = (struct compat_snd_rawmidi_status64_x86_32) { + .stream = status.stream, + .tstamp_sec = status.tstamp_sec, + .tstamp_nsec = status.tstamp_nsec, + .avail = status.avail, + .xruns = status.xruns, + }; + + if (copy_to_user(src, &compat_status, sizeof(*src))) return -EFAULT; return 0; } -#endif /* CONFIG_X86_X32 */ +#endif enum { SNDRV_RAWMIDI_IOCTL_PARAMS32 = _IOWR('W', 0x10, struct snd_rawmidi_params32), - SNDRV_RAWMIDI_IOCTL_STATUS32 = _IOWR('W', 0x20, struct snd_rawmidi_status32), -#ifdef CONFIG_X86_X32 - SNDRV_RAWMIDI_IOCTL_STATUS_X32 = _IOWR('W', 0x20, struct snd_rawmidi_status_x32), -#endif /* CONFIG_X86_X32 */ + SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT32 = _IOWR('W', 0x20, struct compat_snd_rawmidi_status32), + SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT64 = _IOWR('W', 0x20, struct compat_snd_rawmidi_status64), +#ifdef IA32_EMULATION + SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT64_X86_32 = _IOWR('W', 0x20, struct compat_snd_rawmidi_status64_x86_32), +#endif }; static long snd_rawmidi_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) @@ -161,12 +224,14 @@ static long snd_rawmidi_ioctl_compat(struct file *file, unsigned int cmd, unsign return snd_rawmidi_ioctl(file, cmd, (unsigned long)argp); case SNDRV_RAWMIDI_IOCTL_PARAMS32: return snd_rawmidi_ioctl_params_compat(rfile, argp); - case SNDRV_RAWMIDI_IOCTL_STATUS32: + case SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT32: return snd_rawmidi_ioctl_status_compat(rfile, argp); -#ifdef CONFIG_X86_X32 - case SNDRV_RAWMIDI_IOCTL_STATUS_X32: - return snd_rawmidi_ioctl_status_x32(rfile, argp); -#endif /* CONFIG_X86_X32 */ + case SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT64: + return snd_rawmidi_ioctl_status_compat64(rfile, argp); +#ifdef IA32_EMULATION + case SNDRV_RAWMIDI_IOCTL_STATUS_COMPAT64_X86_32: + return snd_rawmidi_ioctl_status_compat64_x86_32(rfile, argp); +#endif } return -ENOIOCTLCMD; }