1 |
commit: 20417eb334c61341e2228a680488aebe06bbcd16 |
2 |
Author: Mike Pagano <mpagano <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon Mar 28 22:50:11 2022 +0000 |
4 |
Commit: Mike Pagano <mpagano <AT> gentoo <DOT> org> |
5 |
CommitDate: Mon Mar 28 22:50:11 2022 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/linux-patches.git/commit/?id=20417eb3 |
7 |
|
8 |
Revert swiotlb: rework fix info leak with DMA_FROM_DEVICE |
9 |
|
10 |
Signed-off-by: Mike Pagano <mpagano <AT> gentoo.org> |
11 |
|
12 |
0000_README | 4 + |
13 |
...rework-fix-info-leak-with-dma_from_device.patch | 187 +++++++++++++++++++++ |
14 |
2 files changed, 191 insertions(+) |
15 |
|
16 |
diff --git a/0000_README b/0000_README |
17 |
index c59e90bb..0aae2d47 100644 |
18 |
--- a/0000_README |
19 |
+++ b/0000_README |
20 |
@@ -183,6 +183,10 @@ Patch: 2000_BT-Check-key-sizes-only-if-Secure-Simple-Pairing-enabled.patch |
21 |
From: https://lore.kernel.org/linux-bluetooth/20190522070540.48895-1-marcel@××××××××.org/raw |
22 |
Desc: Bluetooth: Check key sizes only when Secure Simple Pairing is enabled. See bug #686758 |
23 |
|
24 |
+Patch: 2410_revert-swiotlb-rework-fix-info-leak-with-dma_from_device.patch |
25 |
+From: https://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git |
26 |
+Desc: Revert swiotlb: rework fix info leak with DMA_FROM_DEVICE |
27 |
+ |
28 |
Patch: 2900_tmp513-Fix-build-issue-by-selecting-CONFIG_REG.patch |
29 |
From: https://bugs.gentoo.org/710790 |
30 |
Desc: tmp513 requies REGMAP_I2C to build. Select it by default in Kconfig. See bug #710790. Thanks to Phil Stracchino |
31 |
|
32 |
diff --git a/2410_revert-swiotlb-rework-fix-info-leak-with-dma_from_device.patch b/2410_revert-swiotlb-rework-fix-info-leak-with-dma_from_device.patch |
33 |
new file mode 100644 |
34 |
index 00000000..69476ab1 |
35 |
--- /dev/null |
36 |
+++ b/2410_revert-swiotlb-rework-fix-info-leak-with-dma_from_device.patch |
37 |
@@ -0,0 +1,187 @@ |
38 |
+From bddac7c1e02ba47f0570e494c9289acea3062cc1 Mon Sep 17 00:00:00 2001 |
39 |
+From: Linus Torvalds <torvalds@××××××××××××××××.org> |
40 |
+Date: Sat, 26 Mar 2022 10:42:04 -0700 |
41 |
+Subject: Revert "swiotlb: rework "fix info leak with DMA_FROM_DEVICE"" |
42 |
+MIME-Version: 1.0 |
43 |
+Content-Type: text/plain; charset=UTF-8 |
44 |
+Content-Transfer-Encoding: 8bit |
45 |
+ |
46 |
+From: Linus Torvalds <torvalds@××××××××××××××××.org> |
47 |
+ |
48 |
+commit bddac7c1e02ba47f0570e494c9289acea3062cc1 upstream. |
49 |
+ |
50 |
+This reverts commit aa6f8dcbab473f3a3c7454b74caa46d36cdc5d13. |
51 |
+ |
52 |
+It turns out this breaks at least the ath9k wireless driver, and |
53 |
+possibly others. |
54 |
+ |
55 |
+What the ath9k driver does on packet receive is to set up the DMA |
56 |
+transfer with: |
57 |
+ |
58 |
+ int ath_rx_init(..) |
59 |
+ .. |
60 |
+ bf->bf_buf_addr = dma_map_single(sc->dev, skb->data, |
61 |
+ common->rx_bufsize, |
62 |
+ DMA_FROM_DEVICE); |
63 |
+ |
64 |
+and then the receive logic (through ath_rx_tasklet()) will fetch |
65 |
+incoming packets |
66 |
+ |
67 |
+ static bool ath_edma_get_buffers(..) |
68 |
+ .. |
69 |
+ dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr, |
70 |
+ common->rx_bufsize, DMA_FROM_DEVICE); |
71 |
+ |
72 |
+ ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data); |
73 |
+ if (ret == -EINPROGRESS) { |
74 |
+ /*let device gain the buffer again*/ |
75 |
+ dma_sync_single_for_device(sc->dev, bf->bf_buf_addr, |
76 |
+ common->rx_bufsize, DMA_FROM_DEVICE); |
77 |
+ return false; |
78 |
+ } |
79 |
+ |
80 |
+and it's worth noting how that first DMA sync: |
81 |
+ |
82 |
+ dma_sync_single_for_cpu(..DMA_FROM_DEVICE); |
83 |
+ |
84 |
+is there to make sure the CPU can read the DMA buffer (possibly by |
85 |
+copying it from the bounce buffer area, or by doing some cache flush). |
86 |
+The iommu correctly turns that into a "copy from bounce bufer" so that |
87 |
+the driver can look at the state of the packets. |
88 |
+ |
89 |
+In the meantime, the device may continue to write to the DMA buffer, but |
90 |
+we at least have a snapshot of the state due to that first DMA sync. |
91 |
+ |
92 |
+But that _second_ DMA sync: |
93 |
+ |
94 |
+ dma_sync_single_for_device(..DMA_FROM_DEVICE); |
95 |
+ |
96 |
+is telling the DMA mapping that the CPU wasn't interested in the area |
97 |
+because the packet wasn't there. In the case of a DMA bounce buffer, |
98 |
+that is a no-op. |
99 |
+ |
100 |
+Note how it's not a sync for the CPU (the "for_device()" part), and it's |
101 |
+not a sync for data written by the CPU (the "DMA_FROM_DEVICE" part). |
102 |
+ |
103 |
+Or rather, it _should_ be a no-op. That's what commit aa6f8dcbab47 |
104 |
+broke: it made the code bounce the buffer unconditionally, and changed |
105 |
+the DMA_FROM_DEVICE to just unconditionally and illogically be |
106 |
+DMA_TO_DEVICE. |
107 |
+ |
108 |
+[ Side note: purely within the confines of the swiotlb driver it wasn't |
109 |
+ entirely illogical: The reason it did that odd DMA_FROM_DEVICE -> |
110 |
+ DMA_TO_DEVICE conversion thing is because inside the swiotlb driver, |
111 |
+ it uses just a swiotlb_bounce() helper that doesn't care about the |
112 |
+ whole distinction of who the sync is for - only which direction to |
113 |
+ bounce. |
114 |
+ |
115 |
+ So it took the "sync for device" to mean that the CPU must have been |
116 |
+ the one writing, and thought it meant DMA_TO_DEVICE. ] |
117 |
+ |
118 |
+Also note how the commentary in that commit was wrong, probably due to |
119 |
+that whole confusion, claiming that the commit makes the swiotlb code |
120 |
+ |
121 |
+ "bounce unconditionally (that is, also |
122 |
+ when dir == DMA_TO_DEVICE) in order do avoid synchronising back stale |
123 |
+ data from the swiotlb buffer" |
124 |
+ |
125 |
+which is nonsensical for two reasons: |
126 |
+ |
127 |
+ - that "also when dir == DMA_TO_DEVICE" is nonsensical, as that was |
128 |
+ exactly when it always did - and should do - the bounce. |
129 |
+ |
130 |
+ - since this is a sync for the device (not for the CPU), we're clearly |
131 |
+ fundamentally not coping back stale data from the bounce buffers at |
132 |
+ all, because we'd be copying *to* the bounce buffers. |
133 |
+ |
134 |
+So that commit was just very confused. It confused the direction of the |
135 |
+synchronization (to the device, not the cpu) with the direction of the |
136 |
+DMA (from the device). |
137 |
+ |
138 |
+Reported-and-bisected-by: Oleksandr Natalenko <oleksandr@×××××××××.name> |
139 |
+Reported-by: Olha Cherevyk <olha.cherevyk@×××××.com> |
140 |
+Cc: Halil Pasic <pasic@×××××××××.com> |
141 |
+Cc: Christoph Hellwig <hch@×××.de> |
142 |
+Cc: Kalle Valo <kvalo@××××××.org> |
143 |
+Cc: Robin Murphy <robin.murphy@×××.com> |
144 |
+Cc: Toke Høiland-Jørgensen <toke@××××.dk> |
145 |
+Cc: Maxime Bizon <mbizon@×××××××.fr> |
146 |
+Cc: Johannes Berg <johannes@××××××××××××.net> |
147 |
+Signed-off-by: Linus Torvalds <torvalds@××××××××××××××××.org> |
148 |
+Signed-off-by: Greg Kroah-Hartman <gregkh@×××××××××××××××.org> |
149 |
+--- |
150 |
+ Documentation/core-api/dma-attributes.rst | 8 ++++++++ |
151 |
+ include/linux/dma-mapping.h | 8 ++++++++ |
152 |
+ kernel/dma/swiotlb.c | 23 ++++++++--------------- |
153 |
+ 3 files changed, 24 insertions(+), 15 deletions(-) |
154 |
+ |
155 |
+--- a/Documentation/core-api/dma-attributes.rst |
156 |
++++ b/Documentation/core-api/dma-attributes.rst |
157 |
+@@ -130,3 +130,11 @@ accesses to DMA buffers in both privileg |
158 |
+ subsystem that the buffer is fully accessible at the elevated privilege |
159 |
+ level (and ideally inaccessible or at least read-only at the |
160 |
+ lesser-privileged levels). |
161 |
++ |
162 |
++DMA_ATTR_OVERWRITE |
163 |
++------------------ |
164 |
++ |
165 |
++This is a hint to the DMA-mapping subsystem that the device is expected to |
166 |
++overwrite the entire mapped size, thus the caller does not require any of the |
167 |
++previous buffer contents to be preserved. This allows bounce-buffering |
168 |
++implementations to optimise DMA_FROM_DEVICE transfers. |
169 |
+--- a/include/linux/dma-mapping.h |
170 |
++++ b/include/linux/dma-mapping.h |
171 |
+@@ -62,6 +62,14 @@ |
172 |
+ #define DMA_ATTR_PRIVILEGED (1UL << 9) |
173 |
+ |
174 |
+ /* |
175 |
++ * This is a hint to the DMA-mapping subsystem that the device is expected |
176 |
++ * to overwrite the entire mapped size, thus the caller does not require any |
177 |
++ * of the previous buffer contents to be preserved. This allows |
178 |
++ * bounce-buffering implementations to optimise DMA_FROM_DEVICE transfers. |
179 |
++ */ |
180 |
++#define DMA_ATTR_OVERWRITE (1UL << 10) |
181 |
++ |
182 |
++/* |
183 |
+ * A dma_addr_t can hold any valid DMA or bus address for the platform. It can |
184 |
+ * be given to a device to use as a DMA source or target. It is specific to a |
185 |
+ * given device and there may be a translation between the CPU physical address |
186 |
+--- a/kernel/dma/swiotlb.c |
187 |
++++ b/kernel/dma/swiotlb.c |
188 |
+@@ -627,14 +627,10 @@ phys_addr_t swiotlb_tbl_map_single(struc |
189 |
+ for (i = 0; i < nr_slots(alloc_size + offset); i++) |
190 |
+ mem->slots[index + i].orig_addr = slot_addr(orig_addr, i); |
191 |
+ tlb_addr = slot_addr(mem->start, index) + offset; |
192 |
+- /* |
193 |
+- * When dir == DMA_FROM_DEVICE we could omit the copy from the orig |
194 |
+- * to the tlb buffer, if we knew for sure the device will |
195 |
+- * overwirte the entire current content. But we don't. Thus |
196 |
+- * unconditional bounce may prevent leaking swiotlb content (i.e. |
197 |
+- * kernel memory) to user-space. |
198 |
+- */ |
199 |
+- swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_TO_DEVICE); |
200 |
++ if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && |
201 |
++ (!(attrs & DMA_ATTR_OVERWRITE) || dir == DMA_TO_DEVICE || |
202 |
++ dir == DMA_BIDIRECTIONAL)) |
203 |
++ swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_TO_DEVICE); |
204 |
+ return tlb_addr; |
205 |
+ } |
206 |
+ |
207 |
+@@ -701,13 +697,10 @@ void swiotlb_tbl_unmap_single(struct dev |
208 |
+ void swiotlb_sync_single_for_device(struct device *dev, phys_addr_t tlb_addr, |
209 |
+ size_t size, enum dma_data_direction dir) |
210 |
+ { |
211 |
+- /* |
212 |
+- * Unconditional bounce is necessary to avoid corruption on |
213 |
+- * sync_*_for_cpu or dma_ummap_* when the device didn't overwrite |
214 |
+- * the whole lengt of the bounce buffer. |
215 |
+- */ |
216 |
+- swiotlb_bounce(dev, tlb_addr, size, DMA_TO_DEVICE); |
217 |
+- BUG_ON(!valid_dma_direction(dir)); |
218 |
++ if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL) |
219 |
++ swiotlb_bounce(dev, tlb_addr, size, DMA_TO_DEVICE); |
220 |
++ else |
221 |
++ BUG_ON(dir != DMA_FROM_DEVICE); |
222 |
+ } |
223 |
+ |
224 |
+ void swiotlb_sync_single_for_cpu(struct device *dev, phys_addr_t tlb_addr, |