Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp4548033imm; Tue, 11 Sep 2018 13:44:47 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbbE6r6lmv0nU34Pb/g91b1N5x2nMikH8DjFYVCm0c1YWEtIWx66z8NLSYPaDQS/Z9PIAUu X-Received: by 2002:a63:de4b:: with SMTP id y11-v6mr30225532pgi.435.1536698687531; Tue, 11 Sep 2018 13:44:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536698687; cv=none; d=google.com; s=arc-20160816; b=oFPFla7O20MD5PZMBMv9mb+hA0sj4nwSgCi0zV06Lp/vOhhE7aaeHrmEg4f9v47FI+ eMmY2JWPO4TX8QfKkKp1zTlrKkvoX3FhAa+m+JeHMZPNJsg1UeL1sDR+5d9q/bU1VqgE ErfOThBMd4WHMYwfFUFUB4LIeDBjugfSKUOMBIDJgkeNHdviFoqgPXPbhXNS6mShSCV2 kzTalJ15Zr30bHq8LiCKhW0M6VHglbw4MLo8km19tYADS+ulA5NJJtONGKYHBKbu42f8 CifUwhpc7ebO8mXeyVmxLN6QMYTjIti5yTt+gfQMGGd+xPZNtJoT2yGkFK6RTsm5W4qW B63A== 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:message-id:date :subject:cc:to:from; bh=LSOyIU/WHJ5P4X2ToSrDfH6gYRLvppUK4aozAzMT+SI=; b=iugRr/4nEdX04gpEr9bg1LWCd3/NgEfAfonykkjDeoChI4f8G/Vkmo06TDRl8SdDZv bb7ATG5mSq/y7kg3QrzR+0XRVzq54aWKas3+kYR617/2wd35JcDSR0KpVBDh9Fv6VTG1 lBEXDNy/di1OOusXMXBBYTMm2KGbsFG/TB0yCGzB8QRSdmRToI+myVKZSuRBF35S3Eru yPdWZH/49e31iXbko7Fb7Zln+4HFNsG9HQFtaa9xcbs/QTZNBjQV0lYnAnswdg6CN7lA jmdsTuyszLxU0kbCzcw8KrqQKtamqTCIdtt2cOksRRUglMpNFUkYn89Ox8gatYoh8/dE vfdA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v132-v6si21744706pgb.333.2018.09.11.13.44.32; Tue, 11 Sep 2018 13:44:47 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727746AbeILBpG (ORCPT + 99 others); Tue, 11 Sep 2018 21:45:06 -0400 Received: from mslow2.mail.gandi.net ([217.70.178.242]:42364 "EHLO mslow2.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726836AbeILBpF (ORCPT ); Tue, 11 Sep 2018 21:45:05 -0400 Received: from relay1-d.mail.gandi.net (unknown [217.70.183.193]) by mslow2.mail.gandi.net (Postfix) with ESMTP id 2B0373A7F47 for ; Tue, 11 Sep 2018 22:17:45 +0200 (CEST) X-Originating-IP: 88.190.179.123 Received: from localhost (unknown [88.190.179.123]) (Authenticated sender: repk@triplefau.lt) by relay1-d.mail.gandi.net (Postfix) with ESMTPSA id E3325240003; Tue, 11 Sep 2018 20:17:42 +0000 (UTC) From: Remi Pommarel To: Greg Kroah-Hartman , Jiri Slaby , linux-kernel@vger.kernel.org Cc: Alexander Viro , Kees Cook , Elie Roudninski , Remi Pommarel Subject: [PATCH 1/4] drivers/tty/vt/keyboard.c: refactor getting/setting a keymap entry Date: Tue, 11 Sep 2018 22:23:56 +0200 Message-Id: <3ee65938bc6bcab7c0dc6bf3dd55f7bff41406eb.1536695071.git.repk@triplefau.lt> X-Mailer: git-send-email 2.18.0 In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In order to ease the addition of the ability of an input to use a different key configuration (keycode to keysym map), the operations of getting and setting an entry in a key map is moved in respective functions. Signed-off-by: Remi Pommarel Tested-by: Elie Roudninski --- drivers/tty/vt/keyboard.c | 201 ++++++++++++++++++++++---------------- 1 file changed, 118 insertions(+), 83 deletions(-) diff --git a/drivers/tty/vt/keyboard.c b/drivers/tty/vt/keyboard.c index 88312c6c92cc..89fabb8ae04e 100644 --- a/drivers/tty/vt/keyboard.c +++ b/drivers/tty/vt/keyboard.c @@ -1874,115 +1874,150 @@ int vt_do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, return kc; } -#define i (tmp.kb_index) -#define s (tmp.kb_table) -#define v (tmp.kb_value) +#define i (kbe->kb_index) +#define s (kbe->kb_table) +#define v (kbe->kb_value) -int vt_do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, - int console) +/* + * Get a keysym value from a key map + * + * @kb: Virtual console structure + * @kmaps: Key map array + * @kbe : Entry to find in key map + */ +static ushort __kdsk_getent(struct kbd_struct const *kb, ushort **kmaps, + struct kbentry const *kbe) { - struct kbd_struct *kb = kbd_table + console; - struct kbentry tmp; - ushort *key_map, *new_map, val, ov; + ushort *key_map, val; unsigned long flags; - if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry))) - return -EFAULT; + /* Ensure another thread doesn't free it under us */ + spin_lock_irqsave(&kbd_event_lock, flags); + key_map = kmaps[s]; + if (key_map) { + val = U(key_map[i]); + if (kb->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES) + val = K_HOLE; + } else + val = (i ? K_HOLE : K_NOSUCHMAP); + spin_unlock_irqrestore(&kbd_event_lock, flags); - if (!capable(CAP_SYS_TTY_CONFIG)) - perm = 0; + return val; +} - switch (cmd) { - case KDGKBENT: - /* Ensure another thread doesn't free it under us */ +/* + * Set a keysym value in a key map + * + * @kb: Virtual console structure + * @kmaps: Key map array + * @kbe : Entry to set in key map + */ +static int __kdsk_setent(struct kbd_struct const *kb, ushort **kmaps, + struct kbentry const *kbe) +{ + ushort *key_map, *new_map, ov; + unsigned long flags; + + if (!i && v == K_NOSUCHMAP) { spin_lock_irqsave(&kbd_event_lock, flags); - key_map = key_maps[s]; - if (key_map) { - val = U(key_map[i]); - if (kb->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES) - val = K_HOLE; - } else - val = (i ? K_HOLE : K_NOSUCHMAP); - spin_unlock_irqrestore(&kbd_event_lock, flags); - return put_user(val, &user_kbe->kb_value); - case KDSKBENT: - if (!perm) - return -EPERM; - if (!i && v == K_NOSUCHMAP) { - spin_lock_irqsave(&kbd_event_lock, flags); - /* deallocate map */ - key_map = key_maps[s]; - if (s && key_map) { - key_maps[s] = NULL; - if (key_map[0] == U(K_ALLOCATED)) { - kfree(key_map); - keymap_count--; - } + /* deallocate map */ + key_map = kmaps[s]; + if (s && key_map) { + kmaps[s] = NULL; + if (key_map[0] == U(K_ALLOCATED)) { + kfree(key_map); + keymap_count--; } - spin_unlock_irqrestore(&kbd_event_lock, flags); - break; } + spin_unlock_irqrestore(&kbd_event_lock, flags); + return 0; + } - if (KTYP(v) < NR_TYPES) { - if (KVAL(v) > max_vals[KTYP(v)]) - return -EINVAL; - } else - if (kb->kbdmode != VC_UNICODE) - return -EINVAL; + if (KTYP(v) < NR_TYPES) { + if (KVAL(v) > max_vals[KTYP(v)]) + return -EINVAL; + } else if (kb->kbdmode != VC_UNICODE) + return -EINVAL; - /* ++Geert: non-PC keyboards may generate keycode zero */ + /* ++Geert: non-PC keyboards may generate keycode zero */ #if !defined(__mc68000__) && !defined(__powerpc__) - /* assignment to entry 0 only tests validity of args */ - if (!i) - break; + /* assignment to entry 0 only tests validity of args */ + if (!i) + return 0; #endif - new_map = kmalloc(sizeof(plain_map), GFP_KERNEL); - if (!new_map) - return -ENOMEM; - spin_lock_irqsave(&kbd_event_lock, flags); - key_map = key_maps[s]; - if (key_map == NULL) { - int j; - - if (keymap_count >= MAX_NR_OF_USER_KEYMAPS && - !capable(CAP_SYS_RESOURCE)) { - spin_unlock_irqrestore(&kbd_event_lock, flags); - kfree(new_map); - return -EPERM; - } - key_maps[s] = new_map; - key_map = new_map; - key_map[0] = U(K_ALLOCATED); - for (j = 1; j < NR_KEYS; j++) - key_map[j] = U(K_HOLE); - keymap_count++; - } else - kfree(new_map); + new_map = kmalloc(sizeof(plain_map), GFP_KERNEL); + if (!new_map) + return -ENOMEM; + spin_lock_irqsave(&kbd_event_lock, flags); + key_map = kmaps[s]; + if (key_map == NULL) { + int j; - ov = U(key_map[i]); - if (v == ov) - goto out; - /* - * Attention Key. - */ - if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN)) { + if (keymap_count >= MAX_NR_OF_USER_KEYMAPS && + !capable(CAP_SYS_RESOURCE)) { spin_unlock_irqrestore(&kbd_event_lock, flags); + kfree(new_map); return -EPERM; } - key_map[i] = U(v); - if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT)) - do_compute_shiftstate(); -out: + kmaps[s] = new_map; + key_map = new_map; + key_map[0] = U(K_ALLOCATED); + for (j = 1; j < NR_KEYS; j++) + key_map[j] = U(K_HOLE); + keymap_count++; + } else + kfree(new_map); + + ov = U(key_map[i]); + if (v == ov) + goto out; + /* + * Attention Key. + */ + if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN)) { spin_unlock_irqrestore(&kbd_event_lock, flags); - break; + return -EPERM; } + key_map[i] = U(v); + if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT)) + do_compute_shiftstate(); +out: + spin_unlock_irqrestore(&kbd_event_lock, flags); return 0; } #undef i #undef s #undef v +int vt_do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, + int console) +{ + struct kbd_struct *kb = kbd_table + console; + struct kbentry tmp; + ushort val; + int ret = 0; + + if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry))) + return -EFAULT; + + if (!capable(CAP_SYS_TTY_CONFIG)) + perm = 0; + + switch (cmd) { + case KDGKBENT: + val = __kdsk_getent(kb, key_maps, &tmp); + ret = put_user(val, &user_kbe->kb_value); + break; + case KDSKBENT: + if (!perm) + return -EPERM; + ret = __kdsk_setent(kb, key_maps, &tmp); + break; + } + return ret; +} + /* FIXME: This one needs untangling and locking */ int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm) { -- 2.18.0