History

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.

Vincent Bernat committed 13 years ago

Commit d59577b1 parent 5bd30d319 files · +53 −2Browse files

Unified · Split

19 files
  1. Documentation/networking/filter.txt +9 −2
  2. arch/alpha/include/uapi/asm/socket.h +1 −0
  3. arch/avr32/include/uapi/asm/socket.h +2 −0
  4. arch/cris/include/uapi/asm/socket.h +2 −0
  5. arch/frv/include/uapi/asm/socket.h +2 −0
  6. arch/h8300/include/uapi/asm/socket.h +2 −0
  7. arch/ia64/include/uapi/asm/socket.h +2 −0
  8. arch/m32r/include/uapi/asm/socket.h +2 −0
  9. arch/mips/include/uapi/asm/socket.h +1 −0
  10. arch/mn10300/include/uapi/asm/socket.h +2 −0
  11. arch/parisc/include/uapi/asm/socket.h +1 −0
  12. arch/powerpc/include/uapi/asm/socket.h +2 −0
  13. arch/s390/include/uapi/asm/socket.h +2 −0
  14. arch/sparc/include/uapi/asm/socket.h +1 −0
  15. arch/xtensa/include/uapi/asm/socket.h +2 −0
  16. include/net/sock.h +1 −0
  17. include/uapi/asm-generic/socket.h +2 −0
  18. net/core/filter.c +6 −0
  19. net/core/sock.c +11 −0
Documentation/networking/filter.txt+9−2
@@ -17,12 +17,12 @@ creating filters. · 16 unmodified lines
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 · 2 unmodified lines
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+1−0
@@ -77,5 +77,6 @@ · 76 unmodified lines
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+2−0
@@ -70,4 +70,6 @@ · 69 unmodified lines
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+2−0
@@ -72,6 +72,8 @@ · 71 unmodified lines
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+2−0
@@ -70,5 +70,7 @@ · 69 unmodified lines
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+2−0
@@ -70,4 +70,6 @@ · 69 unmodified lines
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+2−0
@@ -79,4 +79,6 @@ · 78 unmodified lines
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+2−0
@@ -70,4 +70,6 @@ · 69 unmodified lines
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+1−0
@@ -90,5 +90,6 @@ To add: #define SO_REUSEPORT 0x0200 /* Allow local address and port reuse. */ · 89 unmodified lines
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+2−0
@@ -70,4 +70,6 @@ · 69 unmodified lines
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/parisc/include/uapi/asm/socket.h+1−0
@@ -69,6 +69,7 @@ · 68 unmodified lines
6969 /* Instruct lower device to use last 4-bytes of skb data as FCS */
7070 #define SO_NOFCS 0x4024
7171
72+#define SO_LOCK_FILTER 0x4025
7273
7374 /* O_NONBLOCK clashes with the bits used for socket types. Therefore we
7475 * have to define SOCK_NONBLOCK to a different value here.
arch/powerpc/include/uapi/asm/socket.h+2−0
@@ -77,4 +77,6 @@ · 76 unmodified lines
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
81+
8082 #endif /* _ASM_POWERPC_SOCKET_H */
arch/s390/include/uapi/asm/socket.h+2−0
@@ -76,4 +76,6 @@ · 75 unmodified lines
7676 /* Instruct lower device to use last 4-bytes of skb data as FCS */
7777 #define SO_NOFCS 43
7878
79+#define SO_LOCK_FILTER 44
80+
7981 #endif /* _ASM_SOCKET_H */
arch/sparc/include/uapi/asm/socket.h+1−0
@@ -66,6 +66,7 @@ · 65 unmodified lines
6666 /* Instruct lower device to use last 4-bytes of skb data as FCS */
6767 #define SO_NOFCS 0x0027
6868
69+#define SO_LOCK_FILTER 0x0028
6970
7071 /* Security levels - as per NRL IPv6 - don't actually do anything */
7172 #define SO_SECURITY_AUTHENTICATION 0x5001
arch/xtensa/include/uapi/asm/socket.h+2−0
@@ -81,4 +81,6 @@ · 80 unmodified lines
8181 /* Instruct lower device to use last 4-bytes of skb data as FCS */
8282 #define SO_NOFCS 43
8383
84+#define SO_LOCK_FILTER 44
85+
8486 #endif /* _XTENSA_SOCKET_H */
include/net/sock.h+1−0
@@ -664,6 +664,7 @@ enum sock_flags { · 663 unmodified lines
664664 * Will use last 4 bytes of packet sent from
665665 * user-space instead.
666666 */
667+ SOCK_FILTER_LOCKED, /* Filter cannot be changed anymore */
667668 };
668669
669670 static inline void sock_copy_flags(struct sock *nsk, struct sock *osk)
include/uapi/asm-generic/socket.h+2−0
@@ -73,4 +73,6 @@ · 72 unmodified lines
7373 /* Instruct lower device to use last 4-bytes of skb data as FCS */
7474 #define SO_NOFCS 43
7575
76+#define SO_LOCK_FILTER 44
77+
7678 #endif /* __ASM_GENERIC_SOCKET_H */
net/core/filter.c+6−0
@@ -721,6 +721,9 @@ int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk) · 720 unmodified lines
721721 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
722722 int err;
723723
724+ if (sock_flag(sk, SOCK_FILTER_LOCKED))
725+ return -EPERM;
726+
724727 /* Make sure new filter is there and in the right amounts. */
725728 if (fprog->filter == NULL)
726729 return -EINVAL;
@@ -757,6 +760,9 @@ int sk_detach_filter(struct sock *sk) · 30 unmodified lines
757760 int ret = -ENOENT;
758761 struct sk_filter *filter;
759762
763+ if (sock_flag(sk, SOCK_FILTER_LOCKED))
764+ return -EPERM;
765+
760766 filter = rcu_dereference_protected(sk->sk_filter,
761767 sock_owned_by_user(sk));
762768 if (filter) {
net/core/sock.c+11−0
@@ -861,6 +861,13 @@ set_rcvbuf: · 860 unmodified lines
861861 ret = sk_detach_filter(sk);
862862 break;
863863
864+ case SO_LOCK_FILTER:
865+ if (sock_flag(sk, SOCK_FILTER_LOCKED) && !valbool)
866+ ret = -EPERM;
867+ else
868+ sock_valbool_flag(sk, SOCK_FILTER_LOCKED, valbool);
869+ break;
870+
864871 case SO_PASSSEC:
865872 if (valbool)
866873 set_bit(SOCK_PASSSEC, &sock->flags);
@@ -1140,6 +1147,10 @@ int sock_getsockopt(struct socket *sock, int level, int optname, · 273 unmodified lines
11401147
11411148 goto lenout;
11421149
1150+ case SO_LOCK_FILTER:
1151+ v.val = sock_flag(sk, SOCK_FILTER_LOCKED);
1152+ break;
1153+
11431154 default:
11441155 return -ENOPROTOOPT;
11451156 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t