Skip to content

Commit eb627e1

Browse files
mjg59xjamesmorris
authored andcommitted
PCI: Lock down BAR access when the kernel is locked down
Any hardware that can potentially generate DMA has to be locked down in order to avoid it being possible for an attacker to modify kernel code, allowing them to circumvent disabled module loading or module signing. Default to paranoid - in future we can potentially relax this for sufficiently IOMMU-isolated devices. Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: Matthew Garrett <mjg59@google.com> Acked-by: Bjorn Helgaas <bhelgaas@google.com> Reviewed-by: Kees Cook <keescook@chromium.org> cc: linux-pci@vger.kernel.org Signed-off-by: James Morris <jmorris@namei.org>
1 parent 38bd94b commit eb627e1

5 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎drivers/pci/pci-sysfs.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,11 @@ static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
903903
unsigned int size = count;
904904
loff_t init_off = off;
905905
u8 *data = (u8 *) buf;
906+
int ret;
907+
908+
ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
909+
if (ret)
910+
return ret;
906911

907912
if (off > dev->cfg_size)
908913
return 0;
@@ -1164,6 +1169,11 @@ static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
11641169
int bar = (unsigned long)attr->private;
11651170
enum pci_mmap_state mmap_type;
11661171
struct resource *res = &pdev->resource[bar];
1172+
int ret;
1173+
1174+
ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
1175+
if (ret)
1176+
return ret;
11671177

11681178
if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
11691179
return -EINVAL;
@@ -1240,6 +1250,12 @@ static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
12401250
struct bin_attribute *attr, char *buf,
12411251
loff_t off, size_t count)
12421252
{
1253+
int ret;
1254+
1255+
ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
1256+
if (ret)
1257+
return ret;
1258+
12431259
return pci_resource_io(filp, kobj, attr, buf, off, count, true);
12441260
}
12451261

‎drivers/pci/proc.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/seq_file.h>
1414
#include <linux/capability.h>
1515
#include <linux/uaccess.h>
16+
#include <linux/security.h>
1617
#include <asm/byteorder.h>
1718
#include "pci.h"
1819

@@ -115,7 +116,11 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,
115116
struct pci_dev *dev = PDE_DATA(ino);
116117
int pos = *ppos;
117118
int size = dev->cfg_size;
118-
int cnt;
119+
int cnt, ret;
120+
121+
ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
122+
if (ret)
123+
return ret;
119124

120125
if (pos >= size)
121126
return 0;
@@ -196,6 +201,10 @@ static long proc_bus_pci_ioctl(struct file *file, unsigned int cmd,
196201
#endif /* HAVE_PCI_MMAP */
197202
int ret = 0;
198203

204+
ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
205+
if (ret)
206+
return ret;
207+
199208
switch (cmd) {
200209
case PCIIOC_CONTROLLER:
201210
ret = pci_domain_nr(dev->bus);
@@ -238,7 +247,8 @@ static int proc_bus_pci_mmap(struct file *file, struct vm_area_struct *vma)
238247
struct pci_filp_private *fpriv = file->private_data;
239248
int i, ret, write_combine = 0, res_bit = IORESOURCE_MEM;
240249

241-
if (!capable(CAP_SYS_RAWIO))
250+
if (!capable(CAP_SYS_RAWIO) ||
251+
security_locked_down(LOCKDOWN_PCI_ACCESS))
242252
return -EPERM;
243253

244254
if (fpriv->mmap_state == pci_mmap_io) {

‎drivers/pci/syscall.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <linux/errno.h>
99
#include <linux/pci.h>
10+
#include <linux/security.h>
1011
#include <linux/syscalls.h>
1112
#include <linux/uaccess.h>
1213
#include "pci.h"
@@ -90,7 +91,8 @@ SYSCALL_DEFINE5(pciconfig_write, unsigned long, bus, unsigned long, dfn,
9091
u32 dword;
9192
int err = 0;
9293

93-
if (!capable(CAP_SYS_ADMIN))
94+
if (!capable(CAP_SYS_ADMIN) ||
95+
security_locked_down(LOCKDOWN_PCI_ACCESS))
9496
return -EPERM;
9597

9698
dev = pci_get_domain_bus_and_slot(0, bus, dfn);

‎include/linux/security.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ enum lockdown_reason {
107107
LOCKDOWN_DEV_MEM,
108108
LOCKDOWN_KEXEC,
109109
LOCKDOWN_HIBERNATION,
110+
LOCKDOWN_PCI_ACCESS,
110111
LOCKDOWN_INTEGRITY_MAX,
111112
LOCKDOWN_CONFIDENTIALITY_MAX,
112113
};

‎security/lockdown/lockdown.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static char *lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1] = {
2222
[LOCKDOWN_DEV_MEM] = "/dev/mem,kmem,port",
2323
[LOCKDOWN_KEXEC] = "kexec of unsigned images",
2424
[LOCKDOWN_HIBERNATION] = "hibernation",
25+
[LOCKDOWN_PCI_ACCESS] = "direct PCI access",
2526
[LOCKDOWN_INTEGRITY_MAX] = "integrity",
2627
[LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality",
2728
};

0 commit comments

Comments
 (0)