2019-05-27 06:55:01 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
|
|
|
|
*/
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/socket.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/in.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/timer.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/sockios.h>
|
|
|
|
#include <linux/net.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <net/ax25.h>
|
|
|
|
#include <linux/inet.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/if_arp.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <net/sock.h>
|
2016-12-24 19:46:01 +00:00
|
|
|
#include <linux/uaccess.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/fcntl.h>
|
2024-05-09 09:36:33 +00:00
|
|
|
#include <linux/list.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
|
2024-05-09 09:36:33 +00:00
|
|
|
static LIST_HEAD(ax25_dev_list);
|
2005-04-16 22:20:36 +00:00
|
|
|
DEFINE_SPINLOCK(ax25_dev_lock);
|
|
|
|
|
|
|
|
ax25_dev *ax25_addr_ax25dev(ax25_address *addr)
|
|
|
|
{
|
|
|
|
ax25_dev *ax25_dev, *res = NULL;
|
|
|
|
|
|
|
|
spin_lock_bh(&ax25_dev_lock);
|
2024-05-09 09:36:33 +00:00
|
|
|
list_for_each_entry(ax25_dev, &ax25_dev_list, list)
|
2021-10-12 15:58:35 +00:00
|
|
|
if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) == 0) {
|
2005-04-16 22:20:36 +00:00
|
|
|
res = ax25_dev;
|
ax25: add refcount in ax25_dev to avoid UAF bugs
If we dereference ax25_dev after we call kfree(ax25_dev) in
ax25_dev_device_down(), it will lead to concurrency UAF bugs.
There are eight syscall functions suffer from UAF bugs, include
ax25_bind(), ax25_release(), ax25_connect(), ax25_ioctl(),
ax25_getname(), ax25_sendmsg(), ax25_getsockopt() and
ax25_info_show().
One of the concurrency UAF can be shown as below:
(USE) | (FREE)
| ax25_device_event
| ax25_dev_device_down
ax25_bind | ...
... | kfree(ax25_dev)
ax25_fillin_cb() | ...
ax25_fillin_cb_from_dev() |
... |
The root cause of UAF bugs is that kfree(ax25_dev) in
ax25_dev_device_down() is not protected by any locks.
When ax25_dev, which there are still pointers point to,
is released, the concurrency UAF bug will happen.
This patch introduces refcount into ax25_dev in order to
guarantee that there are no pointers point to it when ax25_dev
is released.
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
Signed-off-by: David S. Miller <davem@davemloft.net>
2022-01-28 04:47:16 +00:00
|
|
|
ax25_dev_hold(ax25_dev);
|
2024-05-09 09:36:47 +00:00
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
spin_unlock_bh(&ax25_dev_lock);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is called when an interface is brought up. These are
|
|
|
|
* reasonable defaults.
|
|
|
|
*/
|
|
|
|
void ax25_dev_device_up(struct net_device *dev)
|
|
|
|
{
|
|
|
|
ax25_dev *ax25_dev;
|
|
|
|
|
2022-06-16 15:23:33 +00:00
|
|
|
ax25_dev = kzalloc(sizeof(*ax25_dev), GFP_KERNEL);
|
|
|
|
if (!ax25_dev) {
|
2005-04-16 22:20:36 +00:00
|
|
|
printk(KERN_ERR "AX.25: ax25_dev_device_up - out of memory\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
ax25: add refcount in ax25_dev to avoid UAF bugs
If we dereference ax25_dev after we call kfree(ax25_dev) in
ax25_dev_device_down(), it will lead to concurrency UAF bugs.
There are eight syscall functions suffer from UAF bugs, include
ax25_bind(), ax25_release(), ax25_connect(), ax25_ioctl(),
ax25_getname(), ax25_sendmsg(), ax25_getsockopt() and
ax25_info_show().
One of the concurrency UAF can be shown as below:
(USE) | (FREE)
| ax25_device_event
| ax25_dev_device_down
ax25_bind | ...
... | kfree(ax25_dev)
ax25_fillin_cb() | ...
ax25_fillin_cb_from_dev() |
... |
The root cause of UAF bugs is that kfree(ax25_dev) in
ax25_dev_device_down() is not protected by any locks.
When ax25_dev, which there are still pointers point to,
is released, the concurrency UAF bug will happen.
This patch introduces refcount into ax25_dev in order to
guarantee that there are no pointers point to it when ax25_dev
is released.
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
Signed-off-by: David S. Miller <davem@davemloft.net>
2022-01-28 04:47:16 +00:00
|
|
|
refcount_set(&ax25_dev->refcount, 1);
|
2005-04-16 22:20:36 +00:00
|
|
|
ax25_dev->dev = dev;
|
2022-06-16 15:23:33 +00:00
|
|
|
netdev_hold(dev, &ax25_dev->dev_tracker, GFP_KERNEL);
|
2005-04-16 22:20:36 +00:00
|
|
|
ax25_dev->forward = NULL;
|
ax25: Fix ax25 session cleanup problems
There are session cleanup problems in ax25_release() and
ax25_disconnect(). If we setup a session and then disconnect,
the disconnected session is still in "LISTENING" state that
is shown below.
Active AX.25 sockets
Dest Source Device State Vr/Vs Send-Q Recv-Q
DL9SAU-4 DL9SAU-3 ??? LISTENING 000/000 0 0
DL9SAU-3 DL9SAU-4 ??? LISTENING 000/000 0 0
The first reason is caused by del_timer_sync() in ax25_release().
The timers of ax25 are used for correct session cleanup. If we use
ax25_release() to close ax25 sessions and ax25_dev is not null,
the del_timer_sync() functions in ax25_release() will execute.
As a result, the sessions could not be cleaned up correctly,
because the timers have stopped.
In order to solve this problem, this patch adds a device_up flag
in ax25_dev in order to judge whether the device is up. If there
are sessions to be cleaned up, the del_timer_sync() in
ax25_release() will not execute. What's more, we add ax25_cb_del()
in ax25_kill_by_device(), because the timers have been stopped
and there are no functions that could delete ax25_cb if we do not
call ax25_release(). Finally, we reorder the position of
ax25_list_lock in ax25_cb_del() in order to synchronize among
different functions that call ax25_cb_del().
The second reason is caused by improper check in ax25_disconnect().
The incoming ax25 sessions which ax25->sk is null will close
heartbeat timer, because the check "if(!ax25->sk || ..)" is
satisfied. As a result, the session could not be cleaned up properly.
In order to solve this problem, this patch changes the improper
check to "if(ax25->sk && ..)" in ax25_disconnect().
What`s more, the ax25_disconnect() may be called twice, which is
not necessary. For example, ax25_kill_by_device() calls
ax25_disconnect() and sets ax25->state to AX25_STATE_0, but
ax25_release() calls ax25_disconnect() again.
In order to solve this problem, this patch add a check in
ax25_release(). If the flag of ax25->sk equals to SOCK_DEAD,
the ax25_disconnect() in ax25_release() should not be executed.
Fixes: 82e31755e55f ("ax25: Fix UAF bugs in ax25 timers")
Fixes: 8a367e74c012 ("ax25: Fix segfault after sock connection timeout")
Reported-and-tested-by: Thomas Osterried <thomas@osterried.de>
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
Link: https://lore.kernel.org/r/20220530152158.108619-1-duoming@zju.edu.cn
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
2022-05-30 15:21:58 +00:00
|
|
|
ax25_dev->device_up = true;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
ax25_dev->values[AX25_VALUES_IPDEFMODE] = AX25_DEF_IPDEFMODE;
|
|
|
|
ax25_dev->values[AX25_VALUES_AXDEFMODE] = AX25_DEF_AXDEFMODE;
|
|
|
|
ax25_dev->values[AX25_VALUES_BACKOFF] = AX25_DEF_BACKOFF;
|
|
|
|
ax25_dev->values[AX25_VALUES_CONMODE] = AX25_DEF_CONMODE;
|
|
|
|
ax25_dev->values[AX25_VALUES_WINDOW] = AX25_DEF_WINDOW;
|
|
|
|
ax25_dev->values[AX25_VALUES_EWINDOW] = AX25_DEF_EWINDOW;
|
|
|
|
ax25_dev->values[AX25_VALUES_T1] = AX25_DEF_T1;
|
|
|
|
ax25_dev->values[AX25_VALUES_T2] = AX25_DEF_T2;
|
|
|
|
ax25_dev->values[AX25_VALUES_T3] = AX25_DEF_T3;
|
|
|
|
ax25_dev->values[AX25_VALUES_IDLE] = AX25_DEF_IDLE;
|
|
|
|
ax25_dev->values[AX25_VALUES_N2] = AX25_DEF_N2;
|
|
|
|
ax25_dev->values[AX25_VALUES_PACLEN] = AX25_DEF_PACLEN;
|
|
|
|
ax25_dev->values[AX25_VALUES_PROTOCOL] = AX25_DEF_PROTOCOL;
|
2024-05-01 09:29:32 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_AX25_DAMA_SLAVE
|
2005-04-16 22:20:36 +00:00
|
|
|
ax25_dev->values[AX25_VALUES_DS_TIMEOUT]= AX25_DEF_DS_TIMEOUT;
|
2024-05-01 09:29:32 +00:00
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#if defined(CONFIG_AX25_DAMA_SLAVE) || defined(CONFIG_AX25_DAMA_MASTER)
|
2008-02-12 05:38:32 +00:00
|
|
|
ax25_ds_setup_timer(ax25_dev);
|
2005-04-16 22:20:36 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
spin_lock_bh(&ax25_dev_lock);
|
2024-05-09 09:36:33 +00:00
|
|
|
list_add(&ax25_dev->list, &ax25_dev_list);
|
|
|
|
dev->ax25_ptr = ax25_dev;
|
2005-04-16 22:20:36 +00:00
|
|
|
spin_unlock_bh(&ax25_dev_lock);
|
|
|
|
|
2012-04-19 13:34:18 +00:00
|
|
|
ax25_register_dev_sysctl(ax25_dev);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ax25_dev_device_down(struct net_device *dev)
|
|
|
|
{
|
|
|
|
ax25_dev *s, *ax25_dev;
|
|
|
|
|
|
|
|
if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
|
|
|
|
return;
|
|
|
|
|
2012-04-19 13:34:18 +00:00
|
|
|
ax25_unregister_dev_sysctl(ax25_dev);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
spin_lock_bh(&ax25_dev_lock);
|
|
|
|
|
|
|
|
#ifdef CONFIG_AX25_DAMA_SLAVE
|
2024-03-29 01:50:23 +00:00
|
|
|
timer_shutdown_sync(&ax25_dev->dama.slave_timer);
|
2005-04-16 22:20:36 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove any packet forwarding that points to this device.
|
|
|
|
*/
|
2024-05-09 09:36:33 +00:00
|
|
|
list_for_each_entry(s, &ax25_dev_list, list)
|
2005-04-16 22:20:36 +00:00
|
|
|
if (s->forward == dev)
|
|
|
|
s->forward = NULL;
|
|
|
|
|
2024-05-09 09:36:33 +00:00
|
|
|
list_for_each_entry(s, &ax25_dev_list, list) {
|
|
|
|
if (s == ax25_dev) {
|
|
|
|
list_del(&s->list);
|
2024-05-09 09:37:02 +00:00
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-16 06:28:04 +00:00
|
|
|
|
|
|
|
dev->ax25_ptr = NULL;
|
|
|
|
spin_unlock_bh(&ax25_dev_lock);
|
2022-06-08 04:39:55 +00:00
|
|
|
netdev_put(dev, &ax25_dev->dev_tracker);
|
2022-05-16 06:28:04 +00:00
|
|
|
ax25_dev_put(ax25_dev);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ax25_fwd_ioctl(unsigned int cmd, struct ax25_fwd_struct *fwd)
|
|
|
|
{
|
|
|
|
ax25_dev *ax25_dev, *fwd_dev;
|
|
|
|
|
|
|
|
if ((ax25_dev = ax25_addr_ax25dev(&fwd->port_from)) == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case SIOCAX25ADDFWD:
|
2022-02-03 15:08:11 +00:00
|
|
|
fwd_dev = ax25_addr_ax25dev(&fwd->port_to);
|
|
|
|
if (!fwd_dev) {
|
|
|
|
ax25_dev_put(ax25_dev);
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EINVAL;
|
2022-02-03 15:08:11 +00:00
|
|
|
}
|
|
|
|
if (ax25_dev->forward) {
|
|
|
|
ax25_dev_put(fwd_dev);
|
|
|
|
ax25_dev_put(ax25_dev);
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EINVAL;
|
2022-02-03 15:08:11 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
ax25_dev->forward = fwd_dev->dev;
|
ax25: add refcount in ax25_dev to avoid UAF bugs
If we dereference ax25_dev after we call kfree(ax25_dev) in
ax25_dev_device_down(), it will lead to concurrency UAF bugs.
There are eight syscall functions suffer from UAF bugs, include
ax25_bind(), ax25_release(), ax25_connect(), ax25_ioctl(),
ax25_getname(), ax25_sendmsg(), ax25_getsockopt() and
ax25_info_show().
One of the concurrency UAF can be shown as below:
(USE) | (FREE)
| ax25_device_event
| ax25_dev_device_down
ax25_bind | ...
... | kfree(ax25_dev)
ax25_fillin_cb() | ...
ax25_fillin_cb_from_dev() |
... |
The root cause of UAF bugs is that kfree(ax25_dev) in
ax25_dev_device_down() is not protected by any locks.
When ax25_dev, which there are still pointers point to,
is released, the concurrency UAF bug will happen.
This patch introduces refcount into ax25_dev in order to
guarantee that there are no pointers point to it when ax25_dev
is released.
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
Signed-off-by: David S. Miller <davem@davemloft.net>
2022-01-28 04:47:16 +00:00
|
|
|
ax25_dev_put(fwd_dev);
|
2022-02-03 15:08:11 +00:00
|
|
|
ax25_dev_put(ax25_dev);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SIOCAX25DELFWD:
|
2022-02-03 15:08:11 +00:00
|
|
|
if (!ax25_dev->forward) {
|
|
|
|
ax25_dev_put(ax25_dev);
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EINVAL;
|
2022-02-03 15:08:11 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
ax25_dev->forward = NULL;
|
2022-02-03 15:08:11 +00:00
|
|
|
ax25_dev_put(ax25_dev);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-02-03 15:08:11 +00:00
|
|
|
ax25_dev_put(ax25_dev);
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct net_device *ax25_fwd_dev(struct net_device *dev)
|
|
|
|
{
|
|
|
|
ax25_dev *ax25_dev;
|
|
|
|
|
|
|
|
if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
|
|
|
|
return dev;
|
|
|
|
|
|
|
|
if (ax25_dev->forward == NULL)
|
|
|
|
return dev;
|
|
|
|
|
|
|
|
return ax25_dev->forward;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free all memory associated with device structures.
|
|
|
|
*/
|
|
|
|
void __exit ax25_dev_free(void)
|
|
|
|
{
|
2024-05-09 09:36:33 +00:00
|
|
|
ax25_dev *s, *n;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
spin_lock_bh(&ax25_dev_lock);
|
2024-05-09 09:36:33 +00:00
|
|
|
list_for_each_entry_safe(s, n, &ax25_dev_list, list) {
|
|
|
|
netdev_put(s->dev, &s->dev_tracker);
|
|
|
|
list_del(&s->list);
|
2024-05-30 05:17:33 +00:00
|
|
|
ax25_dev_put(s);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
spin_unlock_bh(&ax25_dev_lock);
|
|
|
|
}
|