Skip to content

iptables state generates a 0 position which is invalid in iptables cli #23950

Closed

Description

@neogenix

When I use a state as follows:

 saltstack-output-4505:
     iptables.insert:
         - position: -1
         - chain: OUTPUT
         - proto: tcp
         - connstate: NEW
         - jump: ACCEPT
         - source: 0.0.0.0/0
         - destination: 0.0.0.0/0
         - dport: 4505
         - table: filter
         - match: state
         - save: True

 saltstack-output-4506:
     iptables.insert:
         - position: -1
         - chain: OUTPUT
         - proto: tcp
         - connstate: NEW
         - jump: ACCEPT
         - source: 0.0.0.0/0
         - destination: 0.0.0.0/0
         - dport: 4506
         - table: filter
         - match: state
         - save: True

It generates a cmd like so (when the chain is empty):

2015-05-19 23:33:47,226 [salt.loaded.int.module.cmdmod][INFO    ][17413] Executing command '/sbin/iptables -t filter -C OUTPUT -p tcp -m state --state NEW --dport 4506 --destination 0.0.0.0/0 --source 0.0.0.0/0 --jump ACCEPT ' in directory '/root'
2015-05-19 23:33:47,233 [salt.loaded.int.module.cmdmod][ERROR   ][17413] Command '/sbin/iptables -t filter -C OUTPUT -p tcp -m state --state NEW --dport 4506 --destination 0.0.0.0/0 --source 0.0.0.0/0 --jump ACCEPT ' failed with return code: 1
2015-05-19 23:33:47,233 [salt.loaded.int.module.cmdmod][ERROR   ][17413] output: iptables: Bad rule (does a matching rule exist in that chain?).

2015-05-19 23:33:47,268 [salt.loaded.int.module.cmdmod][INFO    ][17413] Executing command '/sbin/iptables --wait -t filter -I OUTPUT 0 -p tcp -m state --state NEW --dport 4506 --destination 0.0.0.0/0 --source 0.0.0.0/0 --jump ACCEPT ' in directory '/root'
2015-05-19 23:33:47,274 [salt.loaded.int.module.cmdmod][ERROR   ][17413] Command '/sbin/iptables --wait -t filter -I OUTPUT 0 -p tcp -m state --state NEW --dport 4506 --destination 0.0.0.0/0 --source 0.0.0.0/0 --jump ACCEPT ' failed with return code: 2
2015-05-19 23:33:47,275 [salt.loaded.int.module.cmdmod][ERROR   ][17413] output: iptables v1.4.21: Invalid rule number `0'

When I run the cmd from the box itself, the first one fails for some strange reason, but the second one generates a position of 0:

root@salt-minion-01:~# /sbin/iptables --wait -t filter -I OUTPUT 0 -p tcp -m state --state NEW --dport 4506 --destination 0.0.0.0/0 --source 0.0.0.0/0 --jump ACCEPT
iptables v1.4.21: Invalid rule number `0'
Try `iptables -h' or 'iptables --help' for more information.

Activity

  1. neogenix commented on May 20, 2015

    @neogenix
    ContributorAuthor

    Forgot to add some more info, here's my current iptables layout:

    Every 2.0s: iptables -vnL                                                                                                                                                                                                                             Wed May 20 00:01:27 2015
    
    Chain INPUT (policy ACCEPT 16 packets, 956 bytes)
     pkts bytes target     prot opt in     out     source               destination
        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:4505
        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:4506
      669 33078 log-and-reject  all  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination
    
    Chain OUTPUT (policy ACCEPT 19 packets, 10669 bytes)
     pkts bytes target     prot opt in     out     source               destination
    
    Chain log-and-reject (1 references)
     pkts bytes target     prot opt in     out     source               destination
      669 33078 LOG        all  --  *      *       0.0.0.0/0            0.0.0.0/0            limit: avg 1/sec burst 5 LOG flags 0 level 5 prefix "IPTABLES_LOG_AND_REJECT:"
      669 33078 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW reject-with icmp-port-unreachable
    

    Salt Minions Version Report:

    root@salt-minion-01:~# salt-minion --versions-report
                      Salt: 2015.5.0
                    Python: 2.7.9 (default, Apr  2 2015, 15:33:21)
                    Jinja2: 2.7.3
                  M2Crypto: 0.21.1
            msgpack-python: 0.4.2
              msgpack-pure: Not Installed
                  pycrypto: 2.6.1
                   libnacl: Not Installed
                    PyYAML: 3.11
                     ioflo: Not Installed
                     PyZMQ: 14.4.1
                      RAET: Not Installed
                       ZMQ: 4.0.5
                      Mako: 1.0.0
     Debian source package: 2015.5.0+ds-1utopic1
    

    Salt Master Version Report:

    root@salt-master-01:/srv/salt/formulas/common# salt-master --versions-report
                      Salt: 2015.5.0
                    Python: 2.7.9 (default, Apr  2 2015, 15:33:21)
                    Jinja2: 2.7.3
                  M2Crypto: 0.21.1
            msgpack-python: 0.4.2
              msgpack-pure: Not Installed
                  pycrypto: 2.6.1
                   libnacl: Not Installed
                    PyYAML: 3.11
                     ioflo: Not Installed
                     PyZMQ: 14.4.1
                      RAET: Not Installed
                       ZMQ: 4.0.5
                      Mako: 1.0.0
     Debian source package: 2015.5.0+ds-1utopic1
    
  2. neogenix commented on May 20, 2015

    @neogenix
    ContributorAuthor

    In looking through the logs, it first does a -C, which then doesn't work (obviously), and then it tries with the position 0. Position should probably just be modified to 1, and it'll fix itself.

    2015-05-20 00:11:32,996 [salt.state       ][INFO    ][18273] Running state [saltstack-output-4506] at time 00:11:32.995926
    2015-05-20 00:11:32,996 [salt.state       ][INFO    ][18273] Executing state iptables.insert for saltstack-output-4506
    2015-05-20 00:11:33,004 [salt.loaded.int.module.cmdmod][INFO    ][18273] Executing command '/sbin/iptables -t filter -C OUTPUT -p tcp -m state --state NEW --dport 4506 --destination 0.0.0.0/0 --source 0.0.0.0/0 --jump ACCEPT ' in directory '/root'
    2015-05-20 00:11:33,011 [salt.loaded.int.module.cmdmod][ERROR   ][18273] Command '/sbin/iptables -t filter -C OUTPUT -p tcp -m state --state NEW --dport 4506 --destination 0.0.0.0/0 --source 0.0.0.0/0 --jump ACCEPT ' failed with return code: 1
    2015-05-20 00:11:33,011 [salt.loaded.int.module.cmdmod][ERROR   ][18273] output: iptables: Bad rule (does a matching rule exist in that chain?).
    2015-05-20 00:11:33,012 [salt.loaded.int.module.cmdmod][INFO    ][18273] Executing command '/sbin/iptables-save' in directory '/root'
    2015-05-20 00:11:33,019 [salt.loaded.int.module.cmdmod][DEBUG   ][18273] output: # Generated by iptables-save v1.4.21 on Wed May 20 00:11:33 2015
    *mangle
    :PREROUTING ACCEPT [55409:8213910]
    :INPUT ACCEPT [41816:4723449]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [13115:2489253]
    :POSTROUTING ACCEPT [13115:2489253]
    COMMIT
    # Completed on Wed May 20 00:11:33 2015
    # Generated by iptables-save v1.4.21 on Wed May 20 00:11:33 2015
    *nat
    :PREROUTING ACCEPT [15872:3858131]
    :INPUT ACCEPT [2275:366876]
    :OUTPUT ACCEPT [1848:114932]
    :POSTROUTING ACCEPT [1848:114932]
    COMMIT
    # Completed on Wed May 20 00:11:33 2015
    # Generated by iptables-save v1.4.21 on Wed May 20 00:11:33 2015
    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    :log-and-reject - [0:0]
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 4505 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 4506 -j ACCEPT
    -A INPUT -m state --state NEW -j log-and-reject
    -A log-and-reject -m limit --limit 1/sec -j LOG --log-prefix "IPTABLES_LOG_AND_REJECT:" --log-level 5
    -A log-and-reject -m state --state NEW -j REJECT --reject-with icmp-port-unreachable
    COMMIT
    # Completed on Wed May 20 00:11:33 2015
    2015-05-20 00:11:33,048 [salt.loaded.int.module.cmdmod][INFO    ][18273] Executing command '/sbin/iptables --wait -t filter -I OUTPUT 0 -p tcp -m state --state NEW --dport 4506 --destination 0.0.0.0/0 --source 0.0.0.0/0 --jump ACCEPT ' in directory '/root'
    2015-05-20 00:11:33,054 [salt.loaded.int.module.cmdmod][ERROR   ][18273] Command '/sbin/iptables --wait -t filter -I OUTPUT 0 -p tcp -m state --state NEW --dport 4506 --destination 0.0.0.0/0 --source 0.0.0.0/0 --jump ACCEPT ' failed with return code: 2
    2015-05-20 00:11:33,054 [salt.loaded.int.module.cmdmod][ERROR   ][18273] output: iptables v1.4.21: Invalid rule number `0'
    Try `iptables -h' or 'iptables --help' for more information.
    2015-05-20 00:11:33,055 [salt.state       ][ERROR   ][18273] Failed to set iptables rule for saltstack-output-4506.
    Attempted rule was -p tcp -m state --state NEW --dport 4506 --destination 0.0.0.0/0 --source 0.0.0.0/0 --jump ACCEPT
    2015-05-20 00:11:33,055 [salt.state       ][INFO    ][18273] Completed state [saltstack-output-4506] at time 00:11:33.054752
    
  3. techhat commented on May 20, 2015

    @techhat
    Contributor

    Thanks again for the detailed report @neogenix. I agree, changing 0 to 1 seems like the right approach.

  4. added
    bug
    severity-medium
    P3
    on May 20, 2015
  5. added this to the Approved milestone on May 20, 2015
  6. neogenix commented on May 20, 2015

    @neogenix
    ContributorAuthor

    @techhat thanks! I'll see if I can find it shortly, and PR it.

  7. neogenix commented on May 20, 2015

    @neogenix
    ContributorAuthor

    @rallytime and @techhat PR's are in. Fairly simple 2 line fix. Tested on Ubuntu 15.04.

  8. added a commit that references this issue on May 20, 2015
  9. rallytime commented on May 20, 2015

    @rallytime
    Contributor

    Awesome! Thanks @neogenix! I'll close this now since the PR against 2015.5 is merged and the one against develop will be merged when the tests are finished running (or merged forward...either way that code will be there).

  10. neogenix commented on May 21, 2015

    @neogenix
    ContributorAuthor

    @rallytime do you know off hand if they're going to be building an updated 2015.5.x package for Ubuntu?

  11. added a commit that references this issue on May 21, 2015
  12. rallytime commented on May 21, 2015

    @rallytime
    Contributor

    We internally tagged and started testing 2015.5.1 yesterday, but I think we tagged it before your fixed was merged in. Therefore this fix should be in 2015.5.2, which should be about 2-3 weeks out from now for packages.

  13. added
    Platform
    and removed
    Platform
    on May 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3PlatformState-Modulebugseverity-medium

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions