Skip to content

Commit 08aeb7c

Browse files
jarodwilsonMauro Carvalho Chehab
authored and
Mauro Carvalho Chehab
committed
[media] rc: add locking to fix register/show race
When device_add is called in rc_register_device, the rc sysfs nodes show up, and there's a window in which ir-keytable can be launched via udev and trigger a show_protocols call, which runs without various rc_dev fields filled in yet. Add some locking around registration and store/show_protocols to prevent that from happening. The problem manifests thusly: [64692.957872] BUG: unable to handle kernel NULL pointer dereference at 0000000000000090 [64692.957878] IP: [<ffffffffa036a4c1>] show_protocols+0x47/0xf1 [rc_core] [64692.957890] PGD 19cfc7067 PUD 19cfc6067 PMD 0 [64692.957894] Oops: 0000 [rib#1] SMP [64692.957897] last sysfs file: /sys/devices/pci0000:00/0000:00:03.1/usb3/3-1/3-1:1.0/rc/rc2/protocols [64692.957902] CPU 3 [64692.957903] Modules linked in: redrat3(+) ir_lirc_codec lirc_dev ir_sony_decoder ir_jvc_decoder ir_rc6_decoder ir_rc5_decoder rc_hauppauge ir_nec _decoder rc_core ip6t_REJECT nf_conntrack_ipv6 nf_defrag_ipv6 ip6table_filter ip6_tables snd_emu10k1_synth snd_emux_synth snd_seq_virmidi snd_seq_mi di_event snd_seq_midi_emul snd_emu10k1 snd_rawmidi snd_ac97_codec ac97_bus snd_seq snd_pcm snd_seq_device snd_timer snd_page_alloc snd_util_mem pcsp kr tg3 snd_hwdep emu10k1_gp snd amd64_edac_mod gameport edac_core soundcore edac_mce_amd k8temp shpchp i2c_piix4 lm63 e100 mii uinput ipv6 raid0 rai d1 ata_generic firewire_ohci pata_acpi firewire_core crc_itu_t sata_svw pata_serverworks floppy radeon ttm drm_kms_helper drm i2c_algo_bit i2c_core [last unloaded: redrat3] [64692.957949] [64692.957952] Pid: 12265, comm: ir-keytable Tainted: G M W 2.6.39-rc6+ rib#2 empty empty/TYAN Thunder K8HM S3892 [64692.957957] RIP: 0010:[<ffffffffa036a4c1>] [<ffffffffa036a4c1>] show_protocols+0x47/0xf1 [rc_core] [64692.957962] RSP: 0018:ffff880194509e38 EFLAGS: 00010202 [64692.957964] RAX: 0000000000000000 RBX: ffffffffa036d1e0 RCX: ffffffffa036a47a [64692.957966] RDX: ffff88019a84d000 RSI: ffffffffa036d1e0 RDI: ffff88019cf2f3f0 [64692.957969] RBP: ffff880194509e68 R08: 0000000000000002 R09: 0000000000000000 [64692.957971] R10: 0000000000000002 R11: 0000000000001617 R12: ffff88019a84d000 [64692.957973] R13: 0000000000001000 R14: ffff8801944d2e38 R15: ffff88019ce5f190 [64692.957976] FS: 00007f0a30c9a720(0000) GS:ffff88019fc00000(0000) knlGS:0000000000000000 [64692.957979] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [64692.957981] CR2: 0000000000000090 CR3: 000000019a8e0000 CR4: 00000000000006e0 [64692.957983] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [64692.957986] DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000400 [64692.957989] Process ir-keytable (pid: 12265, threadinfo ffff880194508000, task ffff88019a9fc720) [64692.957991] Stack: [64692.957992] 0000000000000002 ffffffffa036d1e0 ffff880194509f58 0000000000001000 [64692.957997] ffff8801944d2e38 ffff88019ce5f190 ffff880194509e98 ffffffff8131484b [64692.958001] ffffffff8118e923 ffffffff810e9b2f ffff880194509e98 ffff8801944d2e18 [64692.958005] Call Trace: [64692.958014] [<ffffffff8131484b>] dev_attr_show+0x27/0x4e [64692.958014] [<ffffffff8118e923>] ? sysfs_read_file+0x94/0x172 [64692.958014] [<ffffffff810e9b2f>] ? __get_free_pages+0x16/0x52 [64692.958014] [<ffffffff8118e94c>] sysfs_read_file+0xbd/0x172 [64692.958014] [<ffffffff8113205e>] vfs_read+0xac/0xf3 [64692.958014] [<ffffffff8113347b>] ? fget_light+0x3a/0xa1 [64692.958014] [<ffffffff811320f2>] sys_read+0x4d/0x74 [64692.958014] [<ffffffff814c19c2>] system_call_fastpath+0x16/0x1b Its a bit difficult to reproduce, but I'm fairly confident this has fixed the problem. Signed-off-by: Jarod Wilson <[email protected]> Signed-off-by: Mauro Carvalho Chehab <[email protected]>
1 parent 51ea629 commit 08aeb7c

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

drivers/media/rc/rc-main.c

+41-6
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,9 @@ static struct {
749749
* it is trigged by reading /sys/class/rc/rc?/protocols.
750750
* It returns the protocol names of supported protocols.
751751
* Enabled protocols are printed in brackets.
752+
*
753+
* dev->lock is taken to guard against races between device
754+
* registration, store_protocols and show_protocols.
752755
*/
753756
static ssize_t show_protocols(struct device *device,
754757
struct device_attribute *mattr, char *buf)
@@ -762,6 +765,8 @@ static ssize_t show_protocols(struct device *device,
762765
if (!dev)
763766
return -EINVAL;
764767

768+
mutex_lock(&dev->lock);
769+
765770
if (dev->driver_type == RC_DRIVER_SCANCODE) {
766771
enabled = dev->rc_map.rc_type;
767772
allowed = dev->allowed_protos;
@@ -784,6 +789,9 @@ static ssize_t show_protocols(struct device *device,
784789
if (tmp != buf)
785790
tmp--;
786791
*tmp = '\n';
792+
793+
mutex_unlock(&dev->lock);
794+
787795
return tmp + 1 - buf;
788796
}
789797

@@ -802,6 +810,9 @@ static ssize_t show_protocols(struct device *device,
802810
* Writing "none" will disable all protocols.
803811
* Returns -EINVAL if an invalid protocol combination or unknown protocol name
804812
* is used, otherwise @len.
813+
*
814+
* dev->lock is taken to guard against races between device
815+
* registration, store_protocols and show_protocols.
805816
*/
806817
static ssize_t store_protocols(struct device *device,
807818
struct device_attribute *mattr,
@@ -815,18 +826,22 @@ static ssize_t store_protocols(struct device *device,
815826
u64 mask;
816827
int rc, i, count = 0;
817828
unsigned long flags;
829+
ssize_t ret;
818830

819831
/* Device is being removed */
820832
if (!dev)
821833
return -EINVAL;
822834

835+
mutex_lock(&dev->lock);
836+
823837
if (dev->driver_type == RC_DRIVER_SCANCODE)
824838
type = dev->rc_map.rc_type;
825839
else if (dev->raw)
826840
type = dev->raw->enabled_protocols;
827841
else {
828842
IR_dprintk(1, "Protocol switching not supported\n");
829-
return -EINVAL;
843+
ret = -EINVAL;
844+
goto out;
830845
}
831846

832847
while ((tmp = strsep((char **) &data, " \n")) != NULL) {
@@ -860,7 +875,8 @@ static ssize_t store_protocols(struct device *device,
860875
}
861876
if (i == ARRAY_SIZE(proto_names)) {
862877
IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
863-
return -EINVAL;
878+
ret = -EINVAL;
879+
goto out;
864880
}
865881
count++;
866882
}
@@ -875,15 +891,17 @@ static ssize_t store_protocols(struct device *device,
875891

876892
if (!count) {
877893
IR_dprintk(1, "Protocol not specified\n");
878-
return -EINVAL;
894+
ret = -EINVAL;
895+
goto out;
879896
}
880897

881898
if (dev->change_protocol) {
882899
rc = dev->change_protocol(dev, type);
883900
if (rc < 0) {
884901
IR_dprintk(1, "Error setting protocols to 0x%llx\n",
885902
(long long)type);
886-
return -EINVAL;
903+
ret = -EINVAL;
904+
goto out;
887905
}
888906
}
889907

@@ -898,7 +916,11 @@ static ssize_t store_protocols(struct device *device,
898916
IR_dprintk(1, "Current protocol(s): 0x%llx\n",
899917
(long long)type);
900918

901-
return len;
919+
ret = len;
920+
921+
out:
922+
mutex_unlock(&dev->lock);
923+
return ret;
902924
}
903925

904926
static void rc_dev_release(struct device *device)
@@ -974,6 +996,7 @@ struct rc_dev *rc_allocate_device(void)
974996

975997
spin_lock_init(&dev->rc_map.lock);
976998
spin_lock_init(&dev->keylock);
999+
mutex_init(&dev->lock);
9771000
setup_timer(&dev->timer_keyup, ir_timer_keyup, (unsigned long)dev);
9781001

9791002
dev->dev.type = &rc_dev_type;
@@ -1019,12 +1042,21 @@ int rc_register_device(struct rc_dev *dev)
10191042
if (dev->close)
10201043
dev->input_dev->close = ir_close;
10211044

1045+
/*
1046+
* Take the lock here, as the device sysfs node will appear
1047+
* when device_add() is called, which may trigger an ir-keytable udev
1048+
* rule, which will in turn call show_protocols and access either
1049+
* dev->rc_map.rc_type or dev->raw->enabled_protocols before it has
1050+
* been initialized.
1051+
*/
1052+
mutex_lock(&dev->lock);
1053+
10221054
dev->devno = (unsigned long)(atomic_inc_return(&devno) - 1);
10231055
dev_set_name(&dev->dev, "rc%ld", dev->devno);
10241056
dev_set_drvdata(&dev->dev, dev);
10251057
rc = device_add(&dev->dev);
10261058
if (rc)
1027-
return rc;
1059+
goto out_unlock;
10281060

10291061
rc = ir_setkeytable(dev, rc_map);
10301062
if (rc)
@@ -1058,6 +1090,7 @@ int rc_register_device(struct rc_dev *dev)
10581090
if (rc < 0)
10591091
goto out_input;
10601092
}
1093+
mutex_unlock(&dev->lock);
10611094

10621095
if (dev->change_protocol) {
10631096
rc = dev->change_protocol(dev, rc_map->rc_type);
@@ -1083,6 +1116,8 @@ int rc_register_device(struct rc_dev *dev)
10831116
ir_free_table(&dev->rc_map);
10841117
out_dev:
10851118
device_del(&dev->dev);
1119+
out_unlock:
1120+
mutex_unlock(&dev->lock);
10861121
return rc;
10871122
}
10881123
EXPORT_SYMBOL_GPL(rc_register_device);

include/media/rc-core.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ enum rc_driver_type {
4040
* @driver_name: name of the hardware driver which registered this device
4141
* @map_name: name of the default keymap
4242
* @rc_map: current scan/key table
43+
* @lock: used to ensure we've filled in all protocol details before
44+
* anyone can call show_protocols or store_protocols
4345
* @devno: unique remote control device number
4446
* @raw: additional data for raw pulse/space devices
4547
* @input_dev: the input child device used to communicate events to userspace
46-
* @driver_type: specifies if protocol decoding is done in hardware or software
48+
* @driver_type: specifies if protocol decoding is done in hardware or software
4749
* @idle: used to keep track of RX state
4850
* @allowed_protos: bitmask with the supported RC_TYPE_* protocols
4951
* @scanmask: some hardware decoders are not capable of providing the full
@@ -86,7 +88,8 @@ struct rc_dev {
8688
struct input_id input_id;
8789
char *driver_name;
8890
const char *map_name;
89-
struct rc_map rc_map;
91+
struct rc_map rc_map;
92+
struct mutex lock;
9093
unsigned long devno;
9194
struct ir_raw_event_ctrl *raw;
9295
struct input_dev *input_dev;

0 commit comments

Comments
 (0)