|
| 1 | +From 46aa45d0fa3e8879ecdca1c156cb2d91194c45e9 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Pavel Shirshov < [email protected]> |
| 3 | +Date: Thu, 12 Dec 2019 13:47:17 -0800 |
| 4 | +Subject: [PATCH 1/1] lldpctl: put a lock around some commands to avoid race |
| 5 | + conditions |
| 6 | + |
| 7 | +--- |
| 8 | + src/client/client.h | 3 +++ |
| 9 | + src/client/commands.c | 58 ++++++++++++++++++++++++++++++++++++++++--- |
| 10 | + src/client/conf.c | 4 +-- |
| 11 | + 3 files changed, 60 insertions(+), 5 deletions(-) |
| 12 | + |
| 13 | +diff --git a/src/client/client.h b/src/client/client.h |
| 14 | +index e3ee352..6c3e30d 100644 |
| 15 | +--- a/src/client/client.h |
| 16 | ++++ b/src/client/client.h |
| 17 | +@@ -62,6 +62,8 @@ extern void add_history (); |
| 18 | + #endif |
| 19 | + #undef NEWLINE |
| 20 | + |
| 21 | ++extern const char *ctlname; |
| 22 | ++ |
| 23 | + /* commands.c */ |
| 24 | + #define NEWLINE "<CR>" |
| 25 | + struct cmd_node; |
| 26 | +@@ -76,6 +78,7 @@ struct cmd_node *commands_new( |
| 27 | + struct cmd_env*, void *), |
| 28 | + void *); |
| 29 | + struct cmd_node* commands_privileged(struct cmd_node *); |
| 30 | ++struct cmd_node* commands_lock(struct cmd_node *); |
| 31 | + struct cmd_node* commands_hidden(struct cmd_node *); |
| 32 | + void commands_free(struct cmd_node *); |
| 33 | + const char *cmdenv_arg(struct cmd_env*); |
| 34 | +diff --git a/src/client/commands.c b/src/client/commands.c |
| 35 | +index beedbf1..58df4a7 100644 |
| 36 | +--- a/src/client/commands.c |
| 37 | ++++ b/src/client/commands.c |
| 38 | +@@ -18,6 +18,9 @@ |
| 39 | + #include "client.h" |
| 40 | + #include <string.h> |
| 41 | + #include <sys/queue.h> |
| 42 | ++#include <sys/types.h> |
| 43 | ++#include <sys/socket.h> |
| 44 | ++#include <sys/un.h> |
| 45 | + |
| 46 | + /** |
| 47 | + * An element of the environment (a key and a value). |
| 48 | +@@ -68,6 +71,7 @@ struct cmd_node { |
| 49 | + const char *token; /**< Token to enter this cnode */ |
| 50 | + const char *doc; /**< Documentation string */ |
| 51 | + int privileged; /**< Privileged command? */ |
| 52 | ++ int lock; /**< Lock required for execution? */ |
| 53 | + int hidden; /**< Hidden command? */ |
| 54 | + |
| 55 | + /** |
| 56 | +@@ -113,6 +117,21 @@ commands_privileged(struct cmd_node *node) |
| 57 | + return node; |
| 58 | + } |
| 59 | + |
| 60 | ++/** |
| 61 | ++ * Make a node accessible only with a lock. |
| 62 | ++ * |
| 63 | ++ * @param node node to use lock to execute |
| 64 | ++ * @return the modified node |
| 65 | ++ * |
| 66 | ++ * The node is modified. It is returned to ease chaining. |
| 67 | ++ */ |
| 68 | ++struct cmd_node* |
| 69 | ++commands_lock(struct cmd_node *node) |
| 70 | ++{ |
| 71 | ++ if (node) node->lock = 1; |
| 72 | ++ return node; |
| 73 | ++} |
| 74 | ++ |
| 75 | + /** |
| 76 | + * Hide a node from help or completion. |
| 77 | + * |
| 78 | +@@ -344,6 +363,7 @@ _commands_execute(struct lldpctl_conn_t *conn, struct writer *w, |
| 79 | + int n, rc = 0, completion = (word != NULL); |
| 80 | + int help = 0; /* Are we asking for help? */ |
| 81 | + int complete = 0; /* Are we asking for possible completions? */ |
| 82 | ++ int needlock = 0; /* Do we need a lock? */ |
| 83 | + struct cmd_env env = { |
| 84 | + .elements = TAILQ_HEAD_INITIALIZER(env.elements), |
| 85 | + .stack = TAILQ_HEAD_INITIALIZER(env.stack), |
| 86 | +@@ -351,6 +371,7 @@ _commands_execute(struct lldpctl_conn_t *conn, struct writer *w, |
| 87 | + .argv = argv, |
| 88 | + .argp = 0 |
| 89 | + }; |
| 90 | ++ static int lockfd = -1; |
| 91 | + cmdenv_push(&env, root); |
| 92 | + if (!completion) |
| 93 | + for (n = 0; n < argc; n++) |
| 94 | +@@ -388,6 +409,7 @@ _commands_execute(struct lldpctl_conn_t *conn, struct writer *w, |
| 95 | + !strcmp(candidate->token, token)) { |
| 96 | + /* Exact match */ |
| 97 | + best = candidate; |
| 98 | ++ needlock = needlock || candidate->lock; |
| 99 | + break; |
| 100 | + } |
| 101 | + if (!best) best = candidate; |
| 102 | +@@ -406,6 +428,7 @@ _commands_execute(struct lldpctl_conn_t *conn, struct writer *w, |
| 103 | + if (!candidate->token && |
| 104 | + CAN_EXECUTE(candidate)) { |
| 105 | + best = candidate; |
| 106 | ++ needlock = needlock || candidate->lock; |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | +@@ -421,9 +444,38 @@ _commands_execute(struct lldpctl_conn_t *conn, struct writer *w, |
| 111 | + |
| 112 | + /* Push and execute */ |
| 113 | + cmdenv_push(&env, best); |
| 114 | +- if (best->execute && best->execute(conn, w, &env, best->arg) != 1) { |
| 115 | +- rc = -1; |
| 116 | +- goto end; |
| 117 | ++ if (best->execute) { |
| 118 | ++ struct sockaddr_un su; |
| 119 | ++ if (needlock) { |
| 120 | ++ if (lockfd == -1) { |
| 121 | ++ log_debug("lldpctl", "reopen %s for locking", ctlname); |
| 122 | ++ if ((lockfd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
| 123 | ++ log_warn("lldpctl", "cannot open for lock %s", ctlname); |
| 124 | ++ rc = -1; |
| 125 | ++ goto end; |
| 126 | ++ } |
| 127 | ++ su.sun_family = AF_UNIX; |
| 128 | ++ strlcpy(su.sun_path, ctlname, sizeof(su.sun_path)); |
| 129 | ++ if (connect(lockfd, (struct sockaddr *)&su, sizeof(struct sockaddr_un)) == -1) { |
| 130 | ++ log_warn("lldpctl", "cannot connect to socket %s", ctlname); |
| 131 | ++ rc = -1; |
| 132 | ++ close(lockfd); lockfd = -1; |
| 133 | ++ goto end; |
| 134 | ++ } |
| 135 | ++ } |
| 136 | ++ if (lockf(lockfd, F_LOCK, 0) == -1) { |
| 137 | ++ log_warn("lldpctl", "cannot get lock on %s", ctlname); |
| 138 | ++ rc = -1; |
| 139 | ++ close(lockfd); lockfd = -1; |
| 140 | ++ goto end; |
| 141 | ++ } |
| 142 | ++ } |
| 143 | ++ rc = best->execute(conn, w, &env, best->arg) != 1 ? -1 : rc; |
| 144 | ++ if (needlock && lockf(lockfd, F_ULOCK, 0) == -1) { |
| 145 | ++ log_warn("lldpctl", "cannot unlock %s", ctlname); |
| 146 | ++ close(lockfd); lockfd = -1; |
| 147 | ++ } |
| 148 | ++ if (rc == -1) goto end; |
| 149 | + } |
| 150 | + env.argp++; |
| 151 | + } |
| 152 | +diff --git a/src/client/conf.c b/src/client/conf.c |
| 153 | +index 1a14981..ba5743f 100644 |
| 154 | +--- a/src/client/conf.c |
| 155 | ++++ b/src/client/conf.c |
| 156 | +@@ -37,8 +37,8 @@ register_commands_configure(struct cmd_node *root) |
| 157 | + "unconfigure", |
| 158 | + "Unconfigure system settings", |
| 159 | + NULL, NULL, NULL); |
| 160 | +- commands_privileged(configure); |
| 161 | +- commands_privileged(unconfigure); |
| 162 | ++ commands_privileged(commands_lock(configure)); |
| 163 | ++ commands_privileged(commands_lock(unconfigure)); |
| 164 | + cmd_restrict_ports(configure); |
| 165 | + cmd_restrict_ports(unconfigure); |
| 166 | + |
| 167 | +-- |
| 168 | +2.17.1.windows.2 |
| 169 | + |
0 commit comments