Skip to content

Commit d59577b

Browse files
vincentbernatdavem330
authored andcommitted
sk-filter: Add ability to lock a socket filter program
While a privileged program can open a raw socket, attach some restrictive filter and drop its privileges (or send the socket to an unprivileged program through some Unix socket), the filter can still be removed or modified by the unprivileged program. This commit adds a socket option to lock the filter (SO_LOCK_FILTER) preventing any modification of a socket filter program. This is similar to OpenBSD BIOCLOCK ioctl on bpf sockets, except even root is not allowed change/drop the filter. The state of the lock can be read with getsockopt(). No error is triggered if the state is not changed. -EPERM is returned when a user tries to remove the lock or to change/remove the filter while the lock is active. The check is done directly in sk_attach_filter() and sk_detach_filter() and does not affect only setsockopt() syscall. Signed-off-by: Vincent Bernat <bernat@luffy.cx> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 5bd30d3 commit d59577b

19 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎Documentation/networking/filter.txt‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ creating filters.
1717

1818
LSF is much simpler than BPF. One does not have to worry about
1919
devices or anything like that. You simply create your filter
20-
code, send it to the kernel via the SO_ATTACH_FILTER ioctl and
20+
code, send it to the kernel via the SO_ATTACH_FILTER option and
2121
if your filter code passes the kernel check on it, you then
2222
immediately begin filtering data on that socket.
2323

2424
You can also detach filters from your socket via the
25-
SO_DETACH_FILTER ioctl. This will probably not be used much
25+
SO_DETACH_FILTER option. This will probably not be used much
2626
since when you close a socket that has a filter on it the
2727
filter is automagically removed. The other less common case
2828
may be adding a different filter on the same socket where you had another
@@ -31,12 +31,19 @@ the old one and placing your new one in its place, assuming your
3131
filter has passed the checks, otherwise if it fails the old filter
3232
will remain on that socket.
3333

34+
SO_LOCK_FILTER option allows to lock the filter attached to a
35+
socket. Once set, a filter cannot be removed or changed. This allows
36+
one process to setup a socket, attach a filter, lock it then drop
37+
privileges and be assured that the filter will be kept until the
38+
socket is closed.
39+
3440
Examples
3541
========
3642

3743
Ioctls-
3844
setsockopt(sockfd, SOL_SOCKET, SO_ATTACH_FILTER, &Filter, sizeof(Filter));
3945
setsockopt(sockfd, SOL_SOCKET, SO_DETACH_FILTER, &value, sizeof(value));
46+
setsockopt(sockfd, SOL_SOCKET, SO_LOCK_FILTER, &value, sizeof(value));
4047

4148
See the BSD bpf.4 manpage and the BSD Packet Filter paper written by
4249
Steven McCanne and Van Jacobson of Lawrence Berkeley Laboratory.

‎arch/alpha/include/uapi/asm/socket.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,6 @@
7777
/* Instruct lower device to use last 4-bytes of skb data as FCS */
7878
#define SO_NOFCS 43
7979

80+
#define SO_LOCK_FILTER 44
8081

8182
#endif /* _UAPI_ASM_SOCKET_H */

‎arch/avr32/include/uapi/asm/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,6 @@
7070
/* Instruct lower device to use last 4-bytes of skb data as FCS */
7171
#define SO_NOFCS 43
7272

73+
#define SO_LOCK_FILTER 44
74+
7375
#endif /* __ASM_AVR32_SOCKET_H */

‎arch/cris/include/uapi/asm/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
/* Instruct lower device to use last 4-bytes of skb data as FCS */
7373
#define SO_NOFCS 43
7474

75+
#define SO_LOCK_FILTER 44
76+
7577
#endif /* _ASM_SOCKET_H */
7678

7779

‎arch/frv/include/uapi/asm/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,7 @@
7070
/* Instruct lower device to use last 4-bytes of skb data as FCS */
7171
#define SO_NOFCS 43
7272

73+
#define SO_LOCK_FILTER 44
74+
7375
#endif /* _ASM_SOCKET_H */
7476

‎arch/h8300/include/uapi/asm/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,6 @@
7070
/* Instruct lower device to use last 4-bytes of skb data as FCS */
7171
#define SO_NOFCS 43
7272

73+
#define SO_LOCK_FILTER 44
74+
7375
#endif /* _ASM_SOCKET_H */

‎arch/ia64/include/uapi/asm/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,6 @@
7979
/* Instruct lower device to use last 4-bytes of skb data as FCS */
8080
#define SO_NOFCS 43
8181

82+
#define SO_LOCK_FILTER 44
83+
8284
#endif /* _ASM_IA64_SOCKET_H */

‎arch/m32r/include/uapi/asm/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,6 @@
7070
/* Instruct lower device to use last 4-bytes of skb data as FCS */
7171
#define SO_NOFCS 43
7272

73+
#define SO_LOCK_FILTER 44
74+
7375
#endif /* _ASM_M32R_SOCKET_H */

‎arch/mips/include/uapi/asm/socket.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,6 @@ To add: #define SO_REUSEPORT 0x0200 /* Allow local address and port reuse. */
9090
/* Instruct lower device to use last 4-bytes of skb data as FCS */
9191
#define SO_NOFCS 43
9292

93+
#define SO_LOCK_FILTER 44
9394

9495
#endif /* _UAPI_ASM_SOCKET_H */

‎arch/mn10300/include/uapi/asm/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,6 @@
7070
/* Instruct lower device to use last 4-bytes of skb data as FCS */
7171
#define SO_NOFCS 43
7272

73+
#define SO_LOCK_FILTER 44
74+
7375
#endif /* _ASM_SOCKET_H */

0 commit comments

Comments
 (0)