Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Align structure used for LSF on a word

The Linux Socket Filter uses a struct sock_fprog composed of a 2 byte
length followed by a pointer. Pad the length field to a word.

Thanks to Stuart Berman for the bug report!
  • Loading branch information...
commit 06dfd932668bdd848d9bc82e1f3861a780090ec1 1 parent 2b86a53
Michael Santos authored
Showing with 11 additions and 2 deletions.
  1. +4 −2 src/packet.erl
  2. +7 −0 src/procket.erl
6 src/packet.erl
View
@@ -316,14 +316,16 @@ filter(Socket, Insn) when is_list(Insn) ->
% Remove or replace filter
unfilter(Socket) ->
Size = erlang:system_info(wordsize),
+ Pad = procket:wordalign(2),
procket:setsockopt(Socket, ?SOL_SOCKET, ?SO_DETACH_FILTER,
- <<0,0,0,0, 0:(Size*8)>>).
+ <<0,0, 0:(Pad*8), 0:(Size*8)>>).
unfilter(Socket, Insn) when is_list(Insn) ->
filter_1(Socket, Insn, ?SO_DETACH_FILTER).
filter_1(Socket, Insn, Optname) ->
+ Pad = procket:wordalign(2),
{ok, Fcode, [Res]} = procket:alloc([
- <<(length(Insn)):4/native-unsigned-integer-unit:8>>,
+ <<(length(Insn)):2/native-unsigned-integer-unit:8, 0:(Pad*8)>>,
{ptr, list_to_binary(Insn)}
]),
case procket:setsockopt(Socket, ?SOL_SOCKET, Optname, Fcode) of
7 src/procket.erl
View
@@ -50,6 +50,7 @@
alloc/1,
buf/1,
memcpy/2,
+ wordalign/1, wordalign/2,
watcher_create/3,
watcher_arm/1,
@@ -396,3 +397,9 @@ ntohs(<<I:32>>) ->
ntohs(I) when is_integer(I) ->
<<N:16>> = <<I:16/native>>,
N.
+
+
+wordalign(Offset) ->
+ wordalign(Offset, erlang:system_info({wordsize, external})).
+wordalign(Offset, Align) ->
+ (Align - (Offset rem Align)) rem Align.
Please sign in to comment.
Something went wrong with that request. Please try again.