1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2020 SUSE
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * Test transmitting data over a PTY/TTY line discipline and reading from the
6*49cdfc7eSAndroid Build Coastguard Worker * virtual netdev created by the line discipline. Also hangup the PTY while
7*49cdfc7eSAndroid Build Coastguard Worker * data is in flight to try to cause a race between the netdev being deleted
8*49cdfc7eSAndroid Build Coastguard Worker * and the discipline receive function writing to the netdev.
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * For SLCAN we check stack data is not leaked in the frame padding
11*49cdfc7eSAndroid Build Coastguard Worker * (CVE-2020-11494).
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * Test flow:
14*49cdfc7eSAndroid Build Coastguard Worker * 1. Create PTY with ldisc X which creates netdev Y
15*49cdfc7eSAndroid Build Coastguard Worker * 2. Open raw packet socket and bind to netdev Y
16*49cdfc7eSAndroid Build Coastguard Worker * 3. Send data on ptmx and read packets from socket
17*49cdfc7eSAndroid Build Coastguard Worker * 4. Hangup while transmission in progress
18*49cdfc7eSAndroid Build Coastguard Worker *
19*49cdfc7eSAndroid Build Coastguard Worker * Note that not all line disciplines call unthrottle when they are ready to
20*49cdfc7eSAndroid Build Coastguard Worker * read more bytes. So it is possible to fill all the write buffers causing
21*49cdfc7eSAndroid Build Coastguard Worker * write to block forever (because once write sleeps it needs unthrottle to
22*49cdfc7eSAndroid Build Coastguard Worker * wake it). So we write with O_NONBLOCK.
23*49cdfc7eSAndroid Build Coastguard Worker *
24*49cdfc7eSAndroid Build Coastguard Worker * Also the max buffer size for PTYs is 8192, so even if the protocol MTU is
25*49cdfc7eSAndroid Build Coastguard Worker * greater everything may still be processed in 8129 byte chunks. At least
26*49cdfc7eSAndroid Build Coastguard Worker * until we are in the netdev code which can have a bigger buffer. Of course
27*49cdfc7eSAndroid Build Coastguard Worker * the MTU still decides exactly where the packet delimiter goes, this just
28*49cdfc7eSAndroid Build Coastguard Worker * concerns choosing the best packet size to cause a race.
29*49cdfc7eSAndroid Build Coastguard Worker *
30*49cdfc7eSAndroid Build Coastguard Worker * Note on line discipline encapsulation formats:
31*49cdfc7eSAndroid Build Coastguard Worker * - For SLIP frames we just write the data followed by a delimiter char
32*49cdfc7eSAndroid Build Coastguard Worker * - SLCAN we write some ASCII described in drivers/net/can/slcan.c which is
33*49cdfc7eSAndroid Build Coastguard Worker * converted to the actual frame by the kernel
34*49cdfc7eSAndroid Build Coastguard Worker */
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
37*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
38*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
39*49cdfc7eSAndroid Build Coastguard Worker #include "tst_buffers.h"
40*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/tty.h"
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker #if defined(HAVE_LINUX_IF_PACKET_H) && defined(HAVE_LINUX_IF_ETHER_H)
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker #include <linux/if_packet.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <linux/if_ether.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <linux/tty.h>
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker /*
49*49cdfc7eSAndroid Build Coastguard Worker * define instead of including <linux/can.h> to support kernel headers
50*49cdfc7eSAndroid Build Coastguard Worker * before change from v4.2-rc1
51*49cdfc7eSAndroid Build Coastguard Worker * a2f11835994e ("can.h: make padding given by gcc explicit").
52*49cdfc7eSAndroid Build Coastguard Worker */
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker #define CAN_MTU (sizeof(struct can_frame))
55*49cdfc7eSAndroid Build Coastguard Worker #define CAN_MAX_DLEN 8
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker typedef uint32_t canid_t;
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker struct can_frame {
60*49cdfc7eSAndroid Build Coastguard Worker canid_t can_id;
61*49cdfc7eSAndroid Build Coastguard Worker uint8_t can_dlc;
62*49cdfc7eSAndroid Build Coastguard Worker uint8_t __pad;
63*49cdfc7eSAndroid Build Coastguard Worker uint8_t __res0;
64*49cdfc7eSAndroid Build Coastguard Worker uint8_t __res1;
65*49cdfc7eSAndroid Build Coastguard Worker uint8_t data[CAN_MAX_DLEN] __attribute__((aligned(8)));
66*49cdfc7eSAndroid Build Coastguard Worker };
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker #include <stddef.h>
69*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
70*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
71*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
72*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
73*49cdfc7eSAndroid Build Coastguard Worker #include <termios.h>
74*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
75*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
76*49cdfc7eSAndroid Build Coastguard Worker #include <net/if.h>
77*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/ioctl.h"
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_stdio.h"
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker #define SLCAN_FRAME "t00185f5f5f5f5f5f5f5f\r"
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker struct ldisc_info {
84*49cdfc7eSAndroid Build Coastguard Worker int n;
85*49cdfc7eSAndroid Build Coastguard Worker char *name;
86*49cdfc7eSAndroid Build Coastguard Worker int mtu;
87*49cdfc7eSAndroid Build Coastguard Worker };
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker static struct ldisc_info ldiscs[] = {
90*49cdfc7eSAndroid Build Coastguard Worker {N_SLIP, "N_SLIP", 8192},
91*49cdfc7eSAndroid Build Coastguard Worker {N_SLCAN, "N_SLCAN", CAN_MTU},
92*49cdfc7eSAndroid Build Coastguard Worker };
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker static int ptmx = -1, pts = -1, sk = -1, mtu, no_check;
95*49cdfc7eSAndroid Build Coastguard Worker
setup(void)96*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
97*49cdfc7eSAndroid Build Coastguard Worker {
98*49cdfc7eSAndroid Build Coastguard Worker int fd = SAFE_OPEN("/dev/ptmx", O_RDWR);
99*49cdfc7eSAndroid Build Coastguard Worker
100*49cdfc7eSAndroid Build Coastguard Worker TEST(ioctl(fd, TIOCVHANGUP));
101*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
102*49cdfc7eSAndroid Build Coastguard Worker
103*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET && TST_ERR == ENOTTY)
104*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF | TTERRNO, "ioctl(TIOCVHANGUP) not supported");
105*49cdfc7eSAndroid Build Coastguard Worker else if (TST_RET)
106*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TTERRNO, "ioctl(TIOCVHANGUP) failed");
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker
set_ldisc(int tty,const struct ldisc_info * ldisc)109*49cdfc7eSAndroid Build Coastguard Worker static int set_ldisc(int tty, const struct ldisc_info *ldisc)
110*49cdfc7eSAndroid Build Coastguard Worker {
111*49cdfc7eSAndroid Build Coastguard Worker TEST(ioctl(tty, TIOCSETD, &ldisc->n));
112*49cdfc7eSAndroid Build Coastguard Worker
113*49cdfc7eSAndroid Build Coastguard Worker if (!TST_RET)
114*49cdfc7eSAndroid Build Coastguard Worker return 0;
115*49cdfc7eSAndroid Build Coastguard Worker
116*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == EINVAL) {
117*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TTERRNO,
118*49cdfc7eSAndroid Build Coastguard Worker "You don't appear to have the %s TTY line discipline",
119*49cdfc7eSAndroid Build Coastguard Worker ldisc->name);
120*49cdfc7eSAndroid Build Coastguard Worker } else {
121*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO,
122*49cdfc7eSAndroid Build Coastguard Worker "Failed to set the %s line discipline", ldisc->name);
123*49cdfc7eSAndroid Build Coastguard Worker }
124*49cdfc7eSAndroid Build Coastguard Worker
125*49cdfc7eSAndroid Build Coastguard Worker return 1;
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker
open_pty(const struct ldisc_info * ldisc)128*49cdfc7eSAndroid Build Coastguard Worker static int open_pty(const struct ldisc_info *ldisc)
129*49cdfc7eSAndroid Build Coastguard Worker {
130*49cdfc7eSAndroid Build Coastguard Worker char pts_path[PATH_MAX];
131*49cdfc7eSAndroid Build Coastguard Worker
132*49cdfc7eSAndroid Build Coastguard Worker ptmx = SAFE_OPEN("/dev/ptmx", O_RDWR);
133*49cdfc7eSAndroid Build Coastguard Worker if (grantpt(ptmx))
134*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "grantpt(ptmx)");
135*49cdfc7eSAndroid Build Coastguard Worker if (unlockpt(ptmx))
136*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "unlockpt(ptmx)");
137*49cdfc7eSAndroid Build Coastguard Worker if (ptsname_r(ptmx, pts_path, sizeof(pts_path)))
138*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "ptsname_r(ptmx, ...)");
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Worker SAFE_FCNTL(ptmx, F_SETFL, O_NONBLOCK);
141*49cdfc7eSAndroid Build Coastguard Worker
142*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "PTS path is %s", pts_path);
143*49cdfc7eSAndroid Build Coastguard Worker pts = SAFE_OPEN(pts_path, O_RDWR);
144*49cdfc7eSAndroid Build Coastguard Worker
145*49cdfc7eSAndroid Build Coastguard Worker return set_ldisc(pts, ldisc);
146*49cdfc7eSAndroid Build Coastguard Worker }
147*49cdfc7eSAndroid Build Coastguard Worker
try_async_write(int fd,const char * data,ssize_t size,ssize_t * done)148*49cdfc7eSAndroid Build Coastguard Worker static ssize_t try_async_write(int fd, const char *data, ssize_t size,
149*49cdfc7eSAndroid Build Coastguard Worker ssize_t *done)
150*49cdfc7eSAndroid Build Coastguard Worker {
151*49cdfc7eSAndroid Build Coastguard Worker ssize_t off = done ? *done : 0;
152*49cdfc7eSAndroid Build Coastguard Worker ssize_t ret = write(fd, data + off, size - off);
153*49cdfc7eSAndroid Build Coastguard Worker
154*49cdfc7eSAndroid Build Coastguard Worker if (ret < 0)
155*49cdfc7eSAndroid Build Coastguard Worker return -(errno != EAGAIN);
156*49cdfc7eSAndroid Build Coastguard Worker
157*49cdfc7eSAndroid Build Coastguard Worker if (!done)
158*49cdfc7eSAndroid Build Coastguard Worker return 1;
159*49cdfc7eSAndroid Build Coastguard Worker
160*49cdfc7eSAndroid Build Coastguard Worker *done += ret;
161*49cdfc7eSAndroid Build Coastguard Worker return *done >= size;
162*49cdfc7eSAndroid Build Coastguard Worker }
163*49cdfc7eSAndroid Build Coastguard Worker
try_async_read(int fd,char * data,ssize_t size,ssize_t * done)164*49cdfc7eSAndroid Build Coastguard Worker static ssize_t try_async_read(int fd, char *data, ssize_t size,
165*49cdfc7eSAndroid Build Coastguard Worker ssize_t *done)
166*49cdfc7eSAndroid Build Coastguard Worker {
167*49cdfc7eSAndroid Build Coastguard Worker ssize_t off = done ? *done : 0;
168*49cdfc7eSAndroid Build Coastguard Worker ssize_t ret = read(fd, data + off, size - off);
169*49cdfc7eSAndroid Build Coastguard Worker
170*49cdfc7eSAndroid Build Coastguard Worker if (ret < 0)
171*49cdfc7eSAndroid Build Coastguard Worker return -(errno != EAGAIN);
172*49cdfc7eSAndroid Build Coastguard Worker
173*49cdfc7eSAndroid Build Coastguard Worker if (!done)
174*49cdfc7eSAndroid Build Coastguard Worker return 1;
175*49cdfc7eSAndroid Build Coastguard Worker
176*49cdfc7eSAndroid Build Coastguard Worker *done += ret;
177*49cdfc7eSAndroid Build Coastguard Worker return *done >= size;
178*49cdfc7eSAndroid Build Coastguard Worker }
179*49cdfc7eSAndroid Build Coastguard Worker
retry_async_write(int fd,const char * data,ssize_t size)180*49cdfc7eSAndroid Build Coastguard Worker static ssize_t retry_async_write(int fd, const char *data, ssize_t size)
181*49cdfc7eSAndroid Build Coastguard Worker {
182*49cdfc7eSAndroid Build Coastguard Worker ssize_t done = 0;
183*49cdfc7eSAndroid Build Coastguard Worker
184*49cdfc7eSAndroid Build Coastguard Worker return TST_RETRY_FUNC(try_async_write(fd, data, size, &done),
185*49cdfc7eSAndroid Build Coastguard Worker TST_RETVAL_NOTNULL);
186*49cdfc7eSAndroid Build Coastguard Worker }
187*49cdfc7eSAndroid Build Coastguard Worker
retry_async_read(int fd,char * data,ssize_t size)188*49cdfc7eSAndroid Build Coastguard Worker static ssize_t retry_async_read(int fd, char *data, ssize_t size)
189*49cdfc7eSAndroid Build Coastguard Worker {
190*49cdfc7eSAndroid Build Coastguard Worker ssize_t done = 0;
191*49cdfc7eSAndroid Build Coastguard Worker
192*49cdfc7eSAndroid Build Coastguard Worker return TST_RETRY_FUNC(try_async_read(fd, data, size, &done),
193*49cdfc7eSAndroid Build Coastguard Worker TST_RETVAL_NOTNULL);
194*49cdfc7eSAndroid Build Coastguard Worker }
195*49cdfc7eSAndroid Build Coastguard Worker
do_pty(const struct ldisc_info * ldisc)196*49cdfc7eSAndroid Build Coastguard Worker static void do_pty(const struct ldisc_info *ldisc)
197*49cdfc7eSAndroid Build Coastguard Worker {
198*49cdfc7eSAndroid Build Coastguard Worker char *data;
199*49cdfc7eSAndroid Build Coastguard Worker ssize_t ret;
200*49cdfc7eSAndroid Build Coastguard Worker size_t len = 0;
201*49cdfc7eSAndroid Build Coastguard Worker
202*49cdfc7eSAndroid Build Coastguard Worker switch (ldisc->n) {
203*49cdfc7eSAndroid Build Coastguard Worker case N_SLIP:
204*49cdfc7eSAndroid Build Coastguard Worker len = mtu;
205*49cdfc7eSAndroid Build Coastguard Worker break;
206*49cdfc7eSAndroid Build Coastguard Worker case N_SLCAN:
207*49cdfc7eSAndroid Build Coastguard Worker len = sizeof(SLCAN_FRAME) - 1;
208*49cdfc7eSAndroid Build Coastguard Worker break;
209*49cdfc7eSAndroid Build Coastguard Worker }
210*49cdfc7eSAndroid Build Coastguard Worker
211*49cdfc7eSAndroid Build Coastguard Worker data = tst_alloc(len);
212*49cdfc7eSAndroid Build Coastguard Worker
213*49cdfc7eSAndroid Build Coastguard Worker switch (ldisc->n) {
214*49cdfc7eSAndroid Build Coastguard Worker case N_SLIP:
215*49cdfc7eSAndroid Build Coastguard Worker memset(data, '_', len - 1);
216*49cdfc7eSAndroid Build Coastguard Worker data[len - 1] = 0300;
217*49cdfc7eSAndroid Build Coastguard Worker break;
218*49cdfc7eSAndroid Build Coastguard Worker case N_SLCAN:
219*49cdfc7eSAndroid Build Coastguard Worker memcpy(data, SLCAN_FRAME, len);
220*49cdfc7eSAndroid Build Coastguard Worker break;
221*49cdfc7eSAndroid Build Coastguard Worker }
222*49cdfc7eSAndroid Build Coastguard Worker
223*49cdfc7eSAndroid Build Coastguard Worker ret = retry_async_write(ptmx, data, len);
224*49cdfc7eSAndroid Build Coastguard Worker if (ret < 0)
225*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "Failed 1st write to PTY");
226*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Wrote PTY %s %d (1)", ldisc->name, ptmx);
227*49cdfc7eSAndroid Build Coastguard Worker
228*49cdfc7eSAndroid Build Coastguard Worker ret = retry_async_write(ptmx, data, len);
229*49cdfc7eSAndroid Build Coastguard Worker if (ret < 0)
230*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "Failed 2nd write to PTY");
231*49cdfc7eSAndroid Build Coastguard Worker
232*49cdfc7eSAndroid Build Coastguard Worker if (tcflush(ptmx, TCIFLUSH))
233*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "tcflush(ptmx, TCIFLUSH)");
234*49cdfc7eSAndroid Build Coastguard Worker
235*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Wrote PTY %s %d (2)", ldisc->name, ptmx);
236*49cdfc7eSAndroid Build Coastguard Worker
237*49cdfc7eSAndroid Build Coastguard Worker ret = retry_async_read(ptmx, data, len);
238*49cdfc7eSAndroid Build Coastguard Worker if (ret < 0)
239*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "Failed read of PTY");
240*49cdfc7eSAndroid Build Coastguard Worker
241*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Read PTY %s %d", ldisc->name, ptmx);
242*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAKE(0);
243*49cdfc7eSAndroid Build Coastguard Worker
244*49cdfc7eSAndroid Build Coastguard Worker while (1) {
245*49cdfc7eSAndroid Build Coastguard Worker if (retry_async_read(ptmx, data, len) < 0)
246*49cdfc7eSAndroid Build Coastguard Worker break;
247*49cdfc7eSAndroid Build Coastguard Worker
248*49cdfc7eSAndroid Build Coastguard Worker if (retry_async_write(ptmx, data, len) < 0)
249*49cdfc7eSAndroid Build Coastguard Worker break;
250*49cdfc7eSAndroid Build Coastguard Worker }
251*49cdfc7eSAndroid Build Coastguard Worker
252*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Transmission on PTY interrupted by hangup");
253*49cdfc7eSAndroid Build Coastguard Worker
254*49cdfc7eSAndroid Build Coastguard Worker tst_free_all();
255*49cdfc7eSAndroid Build Coastguard Worker }
256*49cdfc7eSAndroid Build Coastguard Worker
open_netdev(const struct ldisc_info * ldisc)257*49cdfc7eSAndroid Build Coastguard Worker static void open_netdev(const struct ldisc_info *ldisc)
258*49cdfc7eSAndroid Build Coastguard Worker {
259*49cdfc7eSAndroid Build Coastguard Worker struct ifreq ifreq = { 0 };
260*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_ll lla = { 0 };
261*49cdfc7eSAndroid Build Coastguard Worker
262*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(pts, SIOCGIFNAME, ifreq.ifr_name);
263*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Netdev is %s", ifreq.ifr_name);
264*49cdfc7eSAndroid Build Coastguard Worker
265*49cdfc7eSAndroid Build Coastguard Worker sk = SAFE_SOCKET(PF_PACKET, SOCK_RAW, 0);
266*49cdfc7eSAndroid Build Coastguard Worker
267*49cdfc7eSAndroid Build Coastguard Worker ifreq.ifr_mtu = ldisc->mtu;
268*49cdfc7eSAndroid Build Coastguard Worker if (ioctl(sk, SIOCSIFMTU, &ifreq))
269*49cdfc7eSAndroid Build Coastguard Worker tst_res(TWARN | TERRNO, "Failed to set netdev MTU to maximum");
270*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(sk, SIOCGIFMTU, &ifreq);
271*49cdfc7eSAndroid Build Coastguard Worker mtu = ifreq.ifr_mtu;
272*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Netdev MTU is %d (we set %d)", mtu, ldisc->mtu);
273*49cdfc7eSAndroid Build Coastguard Worker
274*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(sk, SIOCGIFFLAGS, &ifreq);
275*49cdfc7eSAndroid Build Coastguard Worker ifreq.ifr_flags |= IFF_UP | IFF_RUNNING;
276*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(sk, SIOCSIFFLAGS, &ifreq);
277*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(sk, SIOCGIFFLAGS, &ifreq);
278*49cdfc7eSAndroid Build Coastguard Worker
279*49cdfc7eSAndroid Build Coastguard Worker if (!(ifreq.ifr_flags & IFF_UP))
280*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "Netdev did not come up");
281*49cdfc7eSAndroid Build Coastguard Worker
282*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(sk, SIOCGIFINDEX, &ifreq);
283*49cdfc7eSAndroid Build Coastguard Worker
284*49cdfc7eSAndroid Build Coastguard Worker lla.sll_family = PF_PACKET;
285*49cdfc7eSAndroid Build Coastguard Worker lla.sll_protocol = htons(ETH_P_ALL);
286*49cdfc7eSAndroid Build Coastguard Worker lla.sll_ifindex = ifreq.ifr_ifindex;
287*49cdfc7eSAndroid Build Coastguard Worker SAFE_BIND(sk, (struct sockaddr *)&lla, sizeof(struct sockaddr_ll));
288*49cdfc7eSAndroid Build Coastguard Worker
289*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Bound netdev %d to socket %d", ifreq.ifr_ifindex, sk);
290*49cdfc7eSAndroid Build Coastguard Worker }
291*49cdfc7eSAndroid Build Coastguard Worker
check_data(const struct ldisc_info * ldisc,const char * data,ssize_t len)292*49cdfc7eSAndroid Build Coastguard Worker static void check_data(const struct ldisc_info *ldisc,
293*49cdfc7eSAndroid Build Coastguard Worker const char *data, ssize_t len)
294*49cdfc7eSAndroid Build Coastguard Worker {
295*49cdfc7eSAndroid Build Coastguard Worker ssize_t i = 0, j;
296*49cdfc7eSAndroid Build Coastguard Worker struct can_frame frm;
297*49cdfc7eSAndroid Build Coastguard Worker
298*49cdfc7eSAndroid Build Coastguard Worker if (no_check)
299*49cdfc7eSAndroid Build Coastguard Worker return;
300*49cdfc7eSAndroid Build Coastguard Worker
301*49cdfc7eSAndroid Build Coastguard Worker if (ldisc->n == N_SLCAN) {
302*49cdfc7eSAndroid Build Coastguard Worker memcpy(&frm, data, len);
303*49cdfc7eSAndroid Build Coastguard Worker
304*49cdfc7eSAndroid Build Coastguard Worker if (frm.can_id != 1) {
305*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "can_id = %d != 1",
306*49cdfc7eSAndroid Build Coastguard Worker frm.can_id);
307*49cdfc7eSAndroid Build Coastguard Worker no_check = 1;
308*49cdfc7eSAndroid Build Coastguard Worker }
309*49cdfc7eSAndroid Build Coastguard Worker
310*49cdfc7eSAndroid Build Coastguard Worker if (frm.can_dlc != CAN_MAX_DLEN) {
311*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "can_dlc = %d != " TST_TO_STR_(CAN_MAX_DLEN),
312*49cdfc7eSAndroid Build Coastguard Worker frm.can_dlc);
313*49cdfc7eSAndroid Build Coastguard Worker no_check = 1;
314*49cdfc7eSAndroid Build Coastguard Worker }
315*49cdfc7eSAndroid Build Coastguard Worker
316*49cdfc7eSAndroid Build Coastguard Worker i = offsetof(struct can_frame, __pad);
317*49cdfc7eSAndroid Build Coastguard Worker if (frm.__pad != frm.__res0 || frm.__res0 != frm.__res1) {
318*49cdfc7eSAndroid Build Coastguard Worker tst_res_hexd(TFAIL, data + i,
319*49cdfc7eSAndroid Build Coastguard Worker offsetof(struct can_frame, data) - i,
320*49cdfc7eSAndroid Build Coastguard Worker "Padding bytes may contain stack data");
321*49cdfc7eSAndroid Build Coastguard Worker no_check = 1;
322*49cdfc7eSAndroid Build Coastguard Worker }
323*49cdfc7eSAndroid Build Coastguard Worker
324*49cdfc7eSAndroid Build Coastguard Worker i = offsetof(struct can_frame, data);
325*49cdfc7eSAndroid Build Coastguard Worker }
326*49cdfc7eSAndroid Build Coastguard Worker
327*49cdfc7eSAndroid Build Coastguard Worker do {
328*49cdfc7eSAndroid Build Coastguard Worker if (i >= len)
329*49cdfc7eSAndroid Build Coastguard Worker return;
330*49cdfc7eSAndroid Build Coastguard Worker } while (data[i++] == '_');
331*49cdfc7eSAndroid Build Coastguard Worker
332*49cdfc7eSAndroid Build Coastguard Worker j = i--;
333*49cdfc7eSAndroid Build Coastguard Worker
334*49cdfc7eSAndroid Build Coastguard Worker while (j < len && j - i < 65 && data[j++] != '_')
335*49cdfc7eSAndroid Build Coastguard Worker ;
336*49cdfc7eSAndroid Build Coastguard Worker j--;
337*49cdfc7eSAndroid Build Coastguard Worker
338*49cdfc7eSAndroid Build Coastguard Worker tst_res_hexd(TFAIL, data + i, j - i,
339*49cdfc7eSAndroid Build Coastguard Worker "Corrupt data (max 64 of %ld bytes shown): data[%ld..%ld] = ",
340*49cdfc7eSAndroid Build Coastguard Worker len, i, j);
341*49cdfc7eSAndroid Build Coastguard Worker no_check = 1;
342*49cdfc7eSAndroid Build Coastguard Worker
343*49cdfc7eSAndroid Build Coastguard Worker if (no_check)
344*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Will continue test without data checking");
345*49cdfc7eSAndroid Build Coastguard Worker }
346*49cdfc7eSAndroid Build Coastguard Worker
try_sync_read(int fd,char * data,ssize_t size)347*49cdfc7eSAndroid Build Coastguard Worker static ssize_t try_sync_read(int fd, char *data, ssize_t size)
348*49cdfc7eSAndroid Build Coastguard Worker {
349*49cdfc7eSAndroid Build Coastguard Worker ssize_t ret, n = 0;
350*49cdfc7eSAndroid Build Coastguard Worker int retry = mtu;
351*49cdfc7eSAndroid Build Coastguard Worker
352*49cdfc7eSAndroid Build Coastguard Worker while (retry--) {
353*49cdfc7eSAndroid Build Coastguard Worker ret = read(fd, data + n, size - n);
354*49cdfc7eSAndroid Build Coastguard Worker
355*49cdfc7eSAndroid Build Coastguard Worker if (ret < 0)
356*49cdfc7eSAndroid Build Coastguard Worker return ret;
357*49cdfc7eSAndroid Build Coastguard Worker
358*49cdfc7eSAndroid Build Coastguard Worker if ((n += ret) >= size)
359*49cdfc7eSAndroid Build Coastguard Worker return ret;
360*49cdfc7eSAndroid Build Coastguard Worker }
361*49cdfc7eSAndroid Build Coastguard Worker
362*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "Only read %zd of %zd bytes", n, size);
363*49cdfc7eSAndroid Build Coastguard Worker
364*49cdfc7eSAndroid Build Coastguard Worker return n;
365*49cdfc7eSAndroid Build Coastguard Worker }
366*49cdfc7eSAndroid Build Coastguard Worker
try_sync_write(int fd,const char * data,ssize_t size)367*49cdfc7eSAndroid Build Coastguard Worker static ssize_t try_sync_write(int fd, const char *data, ssize_t size)
368*49cdfc7eSAndroid Build Coastguard Worker {
369*49cdfc7eSAndroid Build Coastguard Worker ssize_t ret, n = 0;
370*49cdfc7eSAndroid Build Coastguard Worker int retry = mtu;
371*49cdfc7eSAndroid Build Coastguard Worker
372*49cdfc7eSAndroid Build Coastguard Worker while (retry--) {
373*49cdfc7eSAndroid Build Coastguard Worker ret = write(fd, data + n, size - n);
374*49cdfc7eSAndroid Build Coastguard Worker
375*49cdfc7eSAndroid Build Coastguard Worker if (ret < 0)
376*49cdfc7eSAndroid Build Coastguard Worker return ret;
377*49cdfc7eSAndroid Build Coastguard Worker
378*49cdfc7eSAndroid Build Coastguard Worker if ((n += ret) >= size)
379*49cdfc7eSAndroid Build Coastguard Worker return ret;
380*49cdfc7eSAndroid Build Coastguard Worker }
381*49cdfc7eSAndroid Build Coastguard Worker
382*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "Only wrote %zd of %zd bytes", n, size);
383*49cdfc7eSAndroid Build Coastguard Worker
384*49cdfc7eSAndroid Build Coastguard Worker return n;
385*49cdfc7eSAndroid Build Coastguard Worker }
386*49cdfc7eSAndroid Build Coastguard Worker
read_netdev(const struct ldisc_info * ldisc)387*49cdfc7eSAndroid Build Coastguard Worker static void read_netdev(const struct ldisc_info *ldisc)
388*49cdfc7eSAndroid Build Coastguard Worker {
389*49cdfc7eSAndroid Build Coastguard Worker int rlen, plen = 0;
390*49cdfc7eSAndroid Build Coastguard Worker char *data;
391*49cdfc7eSAndroid Build Coastguard Worker
392*49cdfc7eSAndroid Build Coastguard Worker switch (ldisc->n) {
393*49cdfc7eSAndroid Build Coastguard Worker case N_SLIP:
394*49cdfc7eSAndroid Build Coastguard Worker plen = mtu - 1;
395*49cdfc7eSAndroid Build Coastguard Worker break;
396*49cdfc7eSAndroid Build Coastguard Worker case N_SLCAN:
397*49cdfc7eSAndroid Build Coastguard Worker plen = CAN_MTU;
398*49cdfc7eSAndroid Build Coastguard Worker break;
399*49cdfc7eSAndroid Build Coastguard Worker }
400*49cdfc7eSAndroid Build Coastguard Worker data = tst_alloc(plen);
401*49cdfc7eSAndroid Build Coastguard Worker
402*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Reading from socket %d", sk);
403*49cdfc7eSAndroid Build Coastguard Worker
404*49cdfc7eSAndroid Build Coastguard Worker TEST(try_sync_read(sk, data, plen));
405*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET < 0)
406*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TTERRNO, "Read netdev %s %d (1)", ldisc->name, sk);
407*49cdfc7eSAndroid Build Coastguard Worker check_data(ldisc, data, plen);
408*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Read netdev %s %d (1)", ldisc->name, sk);
409*49cdfc7eSAndroid Build Coastguard Worker
410*49cdfc7eSAndroid Build Coastguard Worker TEST(try_sync_read(sk, data, plen));
411*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET < 0)
412*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TTERRNO, "Read netdev %s %d (2)", ldisc->name, sk);
413*49cdfc7eSAndroid Build Coastguard Worker check_data(ldisc, data, plen);
414*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Read netdev %s %d (2)", ldisc->name, sk);
415*49cdfc7eSAndroid Build Coastguard Worker
416*49cdfc7eSAndroid Build Coastguard Worker TEST(try_sync_write(sk, data, plen));
417*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET < 0)
418*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TTERRNO, "Write netdev %s %d", ldisc->name, sk);
419*49cdfc7eSAndroid Build Coastguard Worker
420*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Write netdev %s %d", ldisc->name, sk);
421*49cdfc7eSAndroid Build Coastguard Worker
422*49cdfc7eSAndroid Build Coastguard Worker while (1) {
423*49cdfc7eSAndroid Build Coastguard Worker if (try_sync_write(sk, data, plen) < 0)
424*49cdfc7eSAndroid Build Coastguard Worker break;
425*49cdfc7eSAndroid Build Coastguard Worker
426*49cdfc7eSAndroid Build Coastguard Worker if ((rlen = try_sync_read(sk, data, plen)) < 0)
427*49cdfc7eSAndroid Build Coastguard Worker break;
428*49cdfc7eSAndroid Build Coastguard Worker check_data(ldisc, data, rlen);
429*49cdfc7eSAndroid Build Coastguard Worker }
430*49cdfc7eSAndroid Build Coastguard Worker
431*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Data transmission on netdev interrupted by hangup");
432*49cdfc7eSAndroid Build Coastguard Worker
433*49cdfc7eSAndroid Build Coastguard Worker close(sk);
434*49cdfc7eSAndroid Build Coastguard Worker tst_free_all();
435*49cdfc7eSAndroid Build Coastguard Worker }
436*49cdfc7eSAndroid Build Coastguard Worker
do_test(unsigned int n)437*49cdfc7eSAndroid Build Coastguard Worker static void do_test(unsigned int n)
438*49cdfc7eSAndroid Build Coastguard Worker {
439*49cdfc7eSAndroid Build Coastguard Worker struct ldisc_info *ldisc = &ldiscs[n];
440*49cdfc7eSAndroid Build Coastguard Worker
441*49cdfc7eSAndroid Build Coastguard Worker if (open_pty(ldisc))
442*49cdfc7eSAndroid Build Coastguard Worker return;
443*49cdfc7eSAndroid Build Coastguard Worker
444*49cdfc7eSAndroid Build Coastguard Worker open_netdev(ldisc);
445*49cdfc7eSAndroid Build Coastguard Worker
446*49cdfc7eSAndroid Build Coastguard Worker if (!SAFE_FORK()) {
447*49cdfc7eSAndroid Build Coastguard Worker read_netdev(ldisc);
448*49cdfc7eSAndroid Build Coastguard Worker return;
449*49cdfc7eSAndroid Build Coastguard Worker }
450*49cdfc7eSAndroid Build Coastguard Worker
451*49cdfc7eSAndroid Build Coastguard Worker if (!SAFE_FORK()) {
452*49cdfc7eSAndroid Build Coastguard Worker do_pty(ldisc);
453*49cdfc7eSAndroid Build Coastguard Worker return;
454*49cdfc7eSAndroid Build Coastguard Worker }
455*49cdfc7eSAndroid Build Coastguard Worker
456*49cdfc7eSAndroid Build Coastguard Worker if (!SAFE_FORK()) {
457*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAIT2(0, 100000);
458*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(pts, TIOCVHANGUP);
459*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Sent hangup ioctl to PTS");
460*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(ptmx, TIOCVHANGUP);
461*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Sent hangup ioctl to PTM");
462*49cdfc7eSAndroid Build Coastguard Worker return;
463*49cdfc7eSAndroid Build Coastguard Worker }
464*49cdfc7eSAndroid Build Coastguard Worker
465*49cdfc7eSAndroid Build Coastguard Worker tst_reap_children();
466*49cdfc7eSAndroid Build Coastguard Worker }
467*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)468*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
469*49cdfc7eSAndroid Build Coastguard Worker {
470*49cdfc7eSAndroid Build Coastguard Worker if (pts >= 0)
471*49cdfc7eSAndroid Build Coastguard Worker ioctl(pts, TIOCVHANGUP);
472*49cdfc7eSAndroid Build Coastguard Worker
473*49cdfc7eSAndroid Build Coastguard Worker if (ptmx >= 0)
474*49cdfc7eSAndroid Build Coastguard Worker ioctl(ptmx, TIOCVHANGUP);
475*49cdfc7eSAndroid Build Coastguard Worker
476*49cdfc7eSAndroid Build Coastguard Worker if (sk >= 0)
477*49cdfc7eSAndroid Build Coastguard Worker close(sk);
478*49cdfc7eSAndroid Build Coastguard Worker
479*49cdfc7eSAndroid Build Coastguard Worker tst_reap_children();
480*49cdfc7eSAndroid Build Coastguard Worker }
481*49cdfc7eSAndroid Build Coastguard Worker
482*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
483*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
484*49cdfc7eSAndroid Build Coastguard Worker .test = do_test,
485*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
486*49cdfc7eSAndroid Build Coastguard Worker .tcnt = 2,
487*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
488*49cdfc7eSAndroid Build Coastguard Worker .needs_checkpoints = 1,
489*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
490*49cdfc7eSAndroid Build Coastguard Worker .tags = (const struct tst_tag[]){
491*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "b9258a2cece4ec1f020715fe3554bc2e360f6264"},
492*49cdfc7eSAndroid Build Coastguard Worker {"CVE", "CVE-2020-11494"},
493*49cdfc7eSAndroid Build Coastguard Worker {}
494*49cdfc7eSAndroid Build Coastguard Worker }
495*49cdfc7eSAndroid Build Coastguard Worker };
496*49cdfc7eSAndroid Build Coastguard Worker
497*49cdfc7eSAndroid Build Coastguard Worker #else
498*49cdfc7eSAndroid Build Coastguard Worker
499*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("Need <linux/if_packet.h> and <linux/if_ether.h>");
500*49cdfc7eSAndroid Build Coastguard Worker
501*49cdfc7eSAndroid Build Coastguard Worker #endif
502