Commit Graph

25 Commits

Author SHA1 Message Date
Chenbo Feng f3b687c40e BACKPORT: ANDROID: Use sk_uid to replace uid get from socket file
Retrieve socket uid from the sk_uid field added to struct sk instead of
read it from sk->socket->file. It prevent the packet been dropped when
the socket file doesn't exist.

Bug: 37524657
Signed-off-by: Chenbo Feng <fengc@google.com>
Change-Id: I502f4e3819a59eb7367d62350a1e1d7d6e5891db
2019-07-27 21:51:34 +02:00
Chenbo Feng a8485c865e fix the deadlock in xt_qtaguid when enable DDEBUG
When DDEBUG is enabled, the prdebug_full_state() function will try to
recursively aquire the spinlock of sock_tag_list and causing deadlock. A
check statement is added before it aquire the spinlock to differentiate
the behavior depend on the caller of the function.

Bug: 36559739
Test: Compile and run test under system/extra/test/iptables/
Change-Id: Ie3397fbaa207e14fe214d47aaf5e8ca1f4a712ee
Signed-off-by: Chenbo Feng <fengc@google.com>
Git-commit: 8cb8a69b83bd577b97e8247aec866e651c5b96f5
Git-repo: https://android.googlesource.com/kernel/common.git
Signed-off-by: Srinivasarao P <spathi@codeaurora.org>
2019-07-27 21:50:42 +02:00
Chenbo Feng 6219dd5a9a ANDROID: Add untag hacks to inet_release function
To prevent protential risk of memory leak caused by closing socket with
out untag it from qtaguid module, the qtaguid module now do not hold any
socket file reference count. Instead, it will increase the sk_refcnt of
the sk struct to prevent a reuse of the socket pointer.  And when a socket
is released. It will delete the tag if the socket is previously tagged so
no more resources is held by xt_qtaguid moudle. A flag is added to the untag
process to prevent possible kernel crash caused by fail to delete
corresponding socket_tag_entry list.
Bug: 36374484
Test: compile and run test under system/extra/test/iptables,
      run cts -m CtsNetTestCases -t android.net.cts.SocketRefCntTest

Signed-off-by: Chenbo Feng <fengc@google.com>
Change-Id: Iea7c3bf0c59b9774a5114af905b2405f6bc9ee52
Git-commit: 7be18b3a94f61e327b054be5f75bd6d3c975fbe7
Git-repo: https://android.googlesource.com/kernel/common.git
Signed-off-by: Srinivasarao P <spathi@codeaurora.org>
2019-07-27 21:50:41 +02:00
Chenbo Feng 33566fe241 ANDROID: qtaguid: Fix the UAF probelm with tag_ref_tree
When multiple threads is trying to tag/delete the same socket at the
same time, there is a chance the tag_ref_entry of the target socket to
be null before the uid_tag_data entry is freed. It is caused by the
ctrl_cmd_tag function where it doesn't correctly grab the spinlocks
when tagging a socket.

Signed-off-by: Chenbo Feng <fengc@google.com>
Bug: 65853158
Change-Id: I5d89885918054cf835370a52bff2d693362ac5f0
Git-repo: https://android.googlesource.com/kernel/msm
Git-commit: a6661da56dc61b67cc65222b71896a775ceb17be
Signed-off-by: Dennis Cagle <dcagle@codeaurora.org>
2018-08-20 12:11:32 +02:00
Mohamad Ayyash 55d81401df Don't show empty tag stats for unprivileged uids
BUG: 27577101
BUG: 27532522

Change-Id: If0c03fa24270cd3683db482a599fc39e9fec1ac9
Signed-off-by: Mohamad Ayyash <mkayyash@google.com>
Git-commit: d85e322ff3bc8d7aa872ad12df6427dd236e540a
Git-repo: https://android.googlesource.com/kernel/common
Signed-off-by: Ravi Kumar Siddojigari <rsiddoji@codeaurora.org>
2016-09-20 23:02:01 -07:00
Mohamad Ayyash afa1ef5fb7 Replace %p with %pK to prevent leaking kernel address
BUG: 27532522
Change-Id: Ic0710a9a8cfc682acd88ecf3bbfeece2d798c4a4
Signed-off-by: Mohamad Ayyash <mkayyash@google.com>
2016-08-23 02:25:24 -07:00
Mohamad Ayyash 78a08e07a2 xt_qtaguid: Use sk_callback_lock read locks before reading sk->sk_socket
It prevents a kernel panic when accessing sk->sk_socket fields due to NULLing sk->sk_socket when sock_orphan is called through
sk_common_release.

Change-Id: I4aa46b4e2d8600e4d4ef8dcdd363aa4e6e5f8433
Signed-off-by: Mohamad Ayyash <mkayyash@google.com>
(cherry picked from commit cdea0ebcb8bcfe57688f6cb692b49e550ebd9796)
Git-commit: aac55d5ed474e8b6b08d85e742727e5dc0b7893c
Git-repo: https://android.googlesource.com/kernel/common.git
[imaund@codeaurora.org: Resolved context conflicts]
Signed-off-by: Ian Maund <imaund@codeaurora.org>
2015-03-19 15:00:13 -07:00
JP Abgrall dd5b1781e9 nf: xt_qtaguid: fix handling for cases where tunnels are used.
* fix skb->dev vs par->in/out
When there is some forwarding going on, it introduces extra state
around devs associated with xt_action_param->in/out and sk_buff->dev.
E.g.
   par->in and par->out are both set, or
   skb->dev and par->out are both set (and different)
This would lead qtaguid to make the wrong assumption about the
direction and update the wrong device stats.
Now we rely more on par->in/out.

* Fix handling when qtaguid is used as "owner"
When qtaguid is used as an owner module, and sk_socket->file is
not there (happens when tunnels are involved), it would
incorrectly do a tag stats update.

* Correct debug messages.

Bug: 11687690
Change-Id: I2b1ff8bd7131969ce9e25f8291d83a6280b3ba7f
Signed-off-by: JP Abgrall <jpa@google.com>
Git-commit: 2b71479d6f5fe8f33b335f713380f72037244395
Git-repo: https://www.codeaurora.org/cgit/quic/la/kernel/mediatek
[imaund@codeaurora.org: Resolved trivial context conflicts.]
Signed-off-by: Ian Maund <imaund@codeaurora.org>
2014-11-06 12:31:39 -08:00
Sherman Yin b9efd921d4 netfilter: fix seq_printf type mismatch warning
The return type of atomic64_read() varies depending on arch.  The
arm64 version is being changed from long long to long in the mainline
for v3.16, causing a seq_printf type mismatch (%llu) in
guid_ctrl_proc_show().

This commit fixes the type mismatch by casting atomic64_read() to u64.

Change-Id: Iae0a6bd4314f5686a9f4fecbe6203e94ec0870de
Signed-off-by: Sherman Yin <shermanyin@gmail.com>
Git-commit: 186197b12c189b9572a9e739d954674af77549ee
Git-repo: https://android.googlesource.com/kernel/common.git
Signed-off-by: Ian Maund <imaund@codeaurora.org>
2014-06-23 15:22:05 -07:00
Greg Hackmann 2d62019dba netfilter: xt_qtaguid: fix memory leak in seq_file handlers
Change-Id: I15b21230d52479d008a00d9e2191dda020f00925
Signed-off-by: Greg Hackmann <ghackmann@google.com>
Git-commit: 2c06cb20454795a38876be5bd93dd6a8ddcc9d98
Git-Repo: https://android.googlesource.com/kernel/common.git
Signed-off-by: Ian Maund <imaund@codeaurora.org>
2014-06-13 12:05:58 -07:00
Subash Abhinov Kasiviswanathan 8b4f8900a0 net: netfilter: fix compilation warnings
Fix compilation warnings due to type casting between pointers and
function pointers which are treated as errors when the configs
CONFIG_NETFILTER_XT_MATCH_QTAGUID and
CONFIG_NETFILTER_XT_MATCH_QUOTA2 are enabled

CRs-fixed: 638281
Change-Id: Idce65bcaddc68cc58e42951c183a8fa78810f28f
Signed-off-by: Subash Abhinov Kasiviswanathan <subashab@codeaurora.org>
2014-05-05 12:00:36 -06:00
Michael Bohan 969b6fc408 netfilter: xt_qtaguid: Protect iface list access with necessary lock
This fixes a bug where the list is accessed without a lock,
potentially causing corruption when there is a race.

Change-Id: I084dcadb537de00a4be6fa09e31ec693443d3986
Signed-off-by: Michael Bohan <mbohan@codeaurora.org>
2013-08-01 10:03:53 -07:00
Arve Hjønnevåg 21dd5d7718 netfilter: xt_qtaguid: 3.10 fixes
Stop using obsolete procfs api.

Signed-off-by: Arve Hjønnevåg <arve@android.com>
2013-07-01 15:52:04 -07:00
JP Abgrall 24d852635b netfilter: xt_qtaguid: fix bad tcp_time_wait sock handling
Since (41063e9 ipv4: Early TCP socket demux), skb's can have an sk which
is not a struct sock but the smaller struct inet_timewait_sock without an
sk->sk_socket. Now we bypass sk_state == TCP_TIME_WAIT

Signed-off-by: JP Abgrall <jpa@google.com>
2013-07-01 15:51:58 -07:00
JP Abgrall 52ae0941a4 netfilter: qtaguid: rate limit some of the printks
Some of the printks are in the packet handling path.
We now ratelimit the very unlikely errors to avoid
kmsg spamming.

Signed-off-by: JP Abgrall <jpa@google.com>
2013-07-01 14:16:23 -07:00
JP Abgrall af4d7c85bc netfilter: xt_qtaguid: Allow tracking loopback
In the past it would always ignore interfaces with loopback addresses.
Now we just treat them like any other.
This also helps with writing tests that check for the presence
of the qtaguid module.

Signed-off-by: JP Abgrall <jpa@google.com>
2013-07-01 14:16:20 -07:00
JP Abgrall 9766450977 netfilter: xt_qtaguid: extend iface stat to report protocols
In the past the iface_stat_fmt would only show global bytes/packets
for the skb-based numbers.
For stall detection in userspace, distinguishing tcp vs other protocols
makes it easier.
Now we report
  ifname total_skb_rx_bytes total_skb_rx_packets total_skb_tx_bytes
  total_skb_tx_packets {rx,tx}_{tcp,udp,ohter}_{bytes,packets}

Bug: 6818637
Signed-off-by: JP Abgrall <jpa@google.com>
2013-07-01 14:16:20 -07:00
JP Abgrall e585d17670 netfilter: xt_qtaguid: remove AID_* dependency for access control
qtaguid limits what can be done with /ctrl and /stats based on group
membership.
This changes removes AID_NET_BW_STATS and AID_NET_BW_ACCT, and picks
up the groups from the gid of the matching proc entry files.

Signed-off-by: JP Abgrall <jpa@google.com>
Change-Id: I42e477adde78a12ed5eb58fbc0b277cdaadb6f94
2013-07-01 14:16:19 -07:00
Pontus Fuchs ef8e4b827d netfilter: qtaguid: Don't BUG_ON if create_if_tag_stat fails
If create_if_tag_stat fails to allocate memory (GFP_ATOMIC) the
following will happen:

qtaguid: iface_stat: tag stat alloc failed
...
kernel BUG at xt_qtaguid.c:1482!

Signed-off-by: Pontus Fuchs <pontus.fuchs@gmail.com>
2013-07-01 14:16:14 -07:00
JP Abgrall cb80c2ec45 netfilter: xt_qtaguid: fix error exit that would keep a spinlock.
qtudev_open() could return with a uid_tag_data_tree_lock held
when an kzalloc(..., GFP_ATOMIC) would fail.
Very unlikely to get triggered AND survive the mayhem of running out of mem.

Signed-off-by: JP Abgrall <jpa@google.com>
2013-07-01 14:16:11 -07:00
JP Abgrall c33bcd3772 netfilter: xt_qtaguid: report only uid tags to non-privileged processes
In the past, a process could only see its own stats (uid-based summary,
and details).
Now we allow any process to see other UIDs uid-based stats, but still
hide the detailed stats.

Change-Id: I7666961ed244ac1d9359c339b048799e5db9facc
Signed-off-by: JP Abgrall <jpa@google.com>
2013-07-01 14:16:05 -07:00
JP Abgrall c90ab94236 netfilter: xt_qtaguid: start tracking iface rx/tx at low level
qtaguid tracks the device stats by monitoring when it goes up and down,
then it gets the dev_stats().
But devs don't correctly report stats (either they don't count headers
symmetrically between rx/tx, or they count internal control messages).

Now qtaguid counts the rx/tx bytes/packets during raw:prerouting and
mangle:postrouting (nat is not available in ipv6).

The results are in
  /proc/net/xt_qtaguid/iface_stat_fmt
which outputs a format line (bash expansion):
  ifname  total_skb_{rx,tx}_{bytes,packets}

Added event counters for pre/post handling.
Added extra ctrl_*() pid/uid debugging.

Change-Id: Id84345d544ad1dd5f63e3842cab229e71d339297
Signed-off-by: JP Abgrall <jpa@google.com>
2013-07-01 13:40:45 -07:00
JP Abgrall 8b041edbc7 netfilter: xt_qtaguid: fix ipv6 protocol lookup
When updating the stats for a given uid it would incorrectly assume
IPV4 and pick up the wrong protocol when IPV6.

Change-Id: Iea4a635012b4123bf7aa93809011b7b2040bb3d5
Signed-off-by: JP Abgrall <jpa@google.com>
2013-07-01 13:40:42 -07:00
JP Abgrall cc53cb8db6 netfilter: qtaguid: initialize a local var to keep compiler happy.
There was a case that might have seemed like new_tag_stat was not
initialized and actually used.
Added comment explaining why it was impossible, and a BUG()
in case the logic gets changed.

Change-Id: I1eddd1b6f754c08a3bf89f7e9427e5dce1dfb081
Signed-off-by: JP Abgrall <jpa@google.com>
2013-07-01 13:40:42 -07:00
JP Abgrall 22aca520a5 netfilter: add xt_qtaguid matching module
This module allows tracking stats at the socket level for given UIDs.
It replaces xt_owner.
If the --uid-owner is not specified, it will just count stats based on
who the skb belongs to. This will even happen on incoming skbs as it
looks into the skb via xt_socket magic to see who owns it.
If an skb is lost, it will be assigned to uid=0.

To control what sockets of what UIDs are tagged by what, one uses:
  echo t $sock_fd $accounting_tag $the_billed_uid \
     > /proc/net/xt_qtaguid/ctrl
 So whenever an skb belongs to a sock_fd, it will be accounted against
   $the_billed_uid
  and matching stats will show up under the uid with the given
   $accounting_tag.

Because the number of allocations for the stats structs is not that big:
  ~500 apps * 32 per app
we'll just do it atomic. This avoids walking lists many times, and
the fancy worker thread handling. Slabs will grow when needed later.

It use netdevice and inetaddr notifications instead of hooks in the core dev
code to track when a device comes and goes. This removes the need for
exposed iface_stat.h.

Put procfs dirs in /proc/net/xt_qtaguid/
  ctrl
  stats
  iface_stat/<iface>/...
The uid stats are obtainable in ./stats.

Change-Id: I01af4fd91c8de651668d3decb76d9bdc1e343919
Signed-off-by: JP Abgrall <jpa@google.com>
2013-07-01 13:40:35 -07:00