diff options
| author | Robert Pluim | 2019-08-07 14:07:07 +0200 |
|---|---|---|
| committer | Robert Pluim | 2019-08-07 14:07:07 +0200 |
| commit | bc1cf28da5532c6052eade7b5d19bb59e7e1f7bf (patch) | |
| tree | fbfebc7b64482a7836d46662645836dfecf02b07 | |
| parent | 76662cc47d0dd1482442914d0b1f5011f0c86c5e (diff) | |
| download | emacs-bc1cf28da5532c6052eade7b5d19bb59e7e1f7bf.tar.gz emacs-bc1cf28da5532c6052eade7b5d19bb59e7e1f7bf.zip | |
Change nsm-should-check to look at local subnets
* lisp/net/nsm.el (nsm-network-same-subnet): New function. Checks
if an ip address is in the same subnet as another one.
(nsm-should-check): Use nsm-network-same-subnet to see if we're
connecting to a local subnet machine. Remove checks for RFC1918 addresses.
* test/lisp/net/nsm-tests.el: New file. Test nsm-should-check functionality.
| -rw-r--r-- | lisp/net/nsm.el | 85 | ||||
| -rw-r--r-- | test/lisp/net/nsm-tests.el | 69 |
2 files changed, 110 insertions, 44 deletions
diff --git a/lisp/net/nsm.el b/lisp/net/nsm.el index b59ea07d8a1..b0eff811617 100644 --- a/lisp/net/nsm.el +++ b/lisp/net/nsm.el | |||
| @@ -204,54 +204,51 @@ SETTINGS are the same as those supplied to each check function. | |||
| 204 | RESULTS is an alist where the keys are the checks run and the | 204 | RESULTS is an alist where the keys are the checks run and the |
| 205 | values the results of the checks.") | 205 | values the results of the checks.") |
| 206 | 206 | ||
| 207 | (defun nsm-network-same-subnet (local-ip mask ip) | ||
| 208 | "Returns t if IP is in the same subnet as LOCAL-IP/MASK. | ||
| 209 | LOCAL-IP, MASK, and IP are specified as vectors of integers, and | ||
| 210 | are expected to have the same length. Works for both IPv4 and | ||
| 211 | IPv6 addresses." | ||
| 212 | (let ((matches t) | ||
| 213 | (length (length local-ip))) | ||
| 214 | (unless (memq length '(4 5 8 9)) | ||
| 215 | (error "Unexpected length of IP address %S" local-ip)) | ||
| 216 | (dotimes (i length) | ||
| 217 | (setq matches (and matches | ||
| 218 | (= | ||
| 219 | (logand (aref local-ip i) | ||
| 220 | (aref mask i)) | ||
| 221 | (logand (aref ip i) | ||
| 222 | (aref mask i)))))) | ||
| 223 | matches)) | ||
| 224 | |||
| 207 | (defun nsm-should-check (host) | 225 | (defun nsm-should-check (host) |
| 208 | "Determines whether NSM should check for TLS problems for HOST. | 226 | "Determines whether NSM should check for TLS problems for HOST. |
| 209 | 227 | ||
| 210 | If `nsm-trust-local-network' is or returns non-nil, and if the | 228 | If `nsm-trust-local-network' is or returns non-nil, and if the |
| 211 | host address is a localhost address, a machine address, a direct | 229 | host address is a localhost address, or in the same subnet as one |
| 212 | link or a private network address, this function returns | 230 | of the local interfaces, this function returns nil. Non-nil |
| 213 | nil. Non-nil otherwise." | 231 | otherwise." |
| 214 | (let* ((address (or (nslookup-host-ipv4 host nil 'vector) | 232 | (let ((addresses (network-lookup-address-info host)) |
| 215 | (nslookup-host-ipv6 host nil 'vector))) | 233 | (network-interface-list (network-interface-list)) |
| 216 | (ipv4? (eq (length address) 4))) | 234 | (off-net t)) |
| 217 | (not | 235 | (when |
| 218 | (or (if ipv4? | 236 | (or (and (functionp nsm-trust-local-network) |
| 219 | (or | 237 | (funcall nsm-trust-local-network)) |
| 220 | ;; (0.x.x.x) this machine | 238 | nsm-trust-local-network) |
| 221 | (eq (aref address 0) 0) | 239 | (mapc |
| 222 | ;; (127.x.x.x) localhost | 240 | (lambda (address) |
| 223 | (eq (aref address 0) 0)) | 241 | (mapc |
| 224 | (or | 242 | (lambda (iface) |
| 225 | ;; (::) IPv6 this machine | 243 | (let ((info (network-interface-info (car iface)))) |
| 226 | (not (cl-mismatch address [0 0 0 0 0 0 0 0])) | 244 | (when |
| 227 | ;; (::1) IPv6 localhost | 245 | (nsm-network-same-subnet (substring (car info) 0 -1) |
| 228 | (not (cl-mismatch address [0 0 0 0 0 0 0 1])))) | 246 | (substring (car (cddr info)) 0 -1) |
| 229 | (and (or (and (functionp nsm-trust-local-network) | 247 | address) |
| 230 | (funcall nsm-trust-local-network)) | 248 | (setq off-net nil)))) |
| 231 | nsm-trust-local-network) | 249 | network-interface-list)) |
| 232 | (if ipv4? | 250 | addresses)) |
| 233 | (or | 251 | off-net)) |
| 234 | ;; (10.x.x.x) private | ||
| 235 | (eq (aref address 0) 10) | ||
| 236 | ;; (172.16.x.x) private | ||
| 237 | (and (eq (aref address 0) 172) | ||
| 238 | (eq (aref address 0) 16)) | ||
| 239 | ;; (192.168.x.x) private | ||
| 240 | (and (eq (aref address 0) 192) | ||
| 241 | (eq (aref address 0) 168)) | ||
| 242 | ;; (198.18.x.x) private | ||
| 243 | (and (eq (aref address 0) 198) | ||
| 244 | (eq (aref address 0) 18)) | ||
| 245 | ;; (169.254.x.x) link-local | ||
| 246 | (and (eq (aref address 0) 169) | ||
| 247 | (eq (aref address 0) 254))) | ||
| 248 | (memq (aref address 0) | ||
| 249 | '( | ||
| 250 | 64512 ;; (fc00::) IPv6 unique local address | ||
| 251 | 64768 ;; (fd00::) IPv6 unique local address | ||
| 252 | 65152 ;; (fe80::) IPv6 link-local | ||
| 253 | ) | ||
| 254 | ))))))) | ||
| 255 | 252 | ||
| 256 | (defun nsm-check-tls-connection (process host port status settings) | 253 | (defun nsm-check-tls-connection (process host port status settings) |
| 257 | "Check TLS connection against potential security problems. | 254 | "Check TLS connection against potential security problems. |
diff --git a/test/lisp/net/nsm-tests.el b/test/lisp/net/nsm-tests.el new file mode 100644 index 00000000000..bf6ac04b527 --- /dev/null +++ b/test/lisp/net/nsm-tests.el | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | ;;; network-stream-tests.el --- tests for network security manager -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2019 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; Author: Robert Pluim <rpluim@gmail.com> | ||
| 6 | |||
| 7 | ;; This file is part of GNU Emacs. | ||
| 8 | |||
| 9 | ;; GNU Emacs is free software: you can redistribute it and/or modify | ||
| 10 | ;; it under the terms of the GNU General Public License as published by | ||
| 11 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 12 | ;; (at your option) any later version. | ||
| 13 | |||
| 14 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 17 | ;; GNU General Public License for more details. | ||
| 18 | |||
| 19 | ;; You should have received a copy of the GNU General Public License | ||
| 20 | ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. | ||
| 21 | |||
| 22 | ;;; Commentary: | ||
| 23 | |||
| 24 | |||
| 25 | ;;; Code: | ||
| 26 | |||
| 27 | (require 'nsm) | ||
| 28 | (eval-when-compile (require 'cl-lib)) | ||
| 29 | |||
| 30 | (ert-deftest nsm-check-local-subnet-ipv4 () | ||
| 31 | "Check that nsm can be avoided for local subnets." | ||
| 32 | (let ((local-ip '[172 26 128 160 0]) | ||
| 33 | (mask '[255 255 255 0 0]) | ||
| 34 | |||
| 35 | (wrong-length-mask '[255 255 255]) | ||
| 36 | (wrong-mask '[255 255 255 255 0]) | ||
| 37 | (remote-ip-yes '[172 26 128 161 0]) | ||
| 38 | (remote-ip-no '[172 26 129 161 0])) | ||
| 39 | |||
| 40 | (should (eq t (nsm-network-same-subnet local-ip mask remote-ip-yes))) | ||
| 41 | (should (eq nil (nsm-network-same-subnet local-ip mask remote-ip-no))) | ||
| 42 | (should-error (nsm-network-same-subnet local-ip wrong-length-mask remote-ip-yes)) | ||
| 43 | (should (eq nil (nsm-network-same-subnet local-ip wrong-mask remote-ip-yes))) | ||
| 44 | (should (eq t (nsm-should-check "google.com"))) | ||
| 45 | (should (eq t (nsm-should-check "127.1"))) | ||
| 46 | (should (eq t (nsm-should-check "localhost"))) | ||
| 47 | (let ((nsm-trust-local-network t)) | ||
| 48 | (should (eq t (nsm-should-check "google.com"))) | ||
| 49 | (should (eq nil (nsm-should-check "127.1"))) | ||
| 50 | (should (eq nil (nsm-should-check "localhost")))))) | ||
| 51 | |||
| 52 | ;; FIXME This will never return true, since | ||
| 53 | ;; network-interface-list only gives the primary address of each | ||
| 54 | ;; interface, which will be the IPv4 one | ||
| 55 | (defun nsm-ipv6-is-available () | ||
| 56 | (and (featurep 'make-network-process '(:family ipv6)) | ||
| 57 | (cl-rassoc-if | ||
| 58 | (lambda (elt) | ||
| 59 | (eq 9 (length elt))) | ||
| 60 | (network-interface-list)))) | ||
| 61 | |||
| 62 | (ert-deftest nsm-check-local-subnet-ipv6 () | ||
| 63 | (skip-unless (nsm-ipv6-is-available)) | ||
| 64 | (should (eq t (nsm-should-check "::1"))) | ||
| 65 | (let ((nsm-trust-local-network t)) | ||
| 66 | (should (eq nil (nsm-should-check "::1"))))) | ||
| 67 | |||
| 68 | |||
| 69 | ;;; nsm-tests.el ends here | ||