summaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
authorMatthew Jordan <mjordan@digium.com>2013-01-02 18:11:59 +0000
committerMatthew Jordan <mjordan@digium.com>2013-01-02 18:11:59 +0000
commit8fb5bdce9ab9f7f3758545753cbc787653920753 (patch)
tree65809194af8a4fa757fef26a30a2cc9c3ed02182 /main
parent1fb06fde957fb466388c764384c2e14698e2dc15 (diff)
Prevent exhaustion of system resources through exploitation of event cache
Asterisk maintains an internal cache for devices in the event subsystem. The device state cache holds the state of each device known to Asterisk, such that consumers of device state information can query for the last known state for a particular device, even if it is not part of an active call. The concept of a device in Asterisk can include entities that do not have a physical representation. One way that this occurred was when anonymous calls are allowed in Asterisk. A device was automatically created and stored in the cache for each anonymous call that occurred; this was possible in the SIP and IAX2 channel drivers and through channel drivers that utilized the res_jabber/res_xmpp resource modules (Gtalk, Jingle, and Motif). These devices are never removed from the system, allowing anonymous calls to potentially exhaust a system's resources. This patch changes the event cache subsystem and device state management to no longer cache devices that are not associated with a physical entity. (issue ASTERISK-20175) Reported by: Russell Bryant, Leif Madsen, Joshua Colp Tested by: kmoore patches: event-cachability-3.diff uploaded by jcolp (license 5000) ........ Merged revisions 378303 from http://svn.asterisk.org/svn/asterisk/branches/1.8 ........ Merged revisions 378320 from http://svn.asterisk.org/svn/asterisk/branches/10 ........ Merged revisions 378321 from http://svn.asterisk.org/svn/asterisk/branches/11 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@378322 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'main')
-rw-r--r--main/ccss.c2
-rw-r--r--main/channel.c4
-rw-r--r--main/channel_internal_api.c1
-rw-r--r--main/devicestate.c51
-rw-r--r--main/event.c2
-rw-r--r--main/features.c2
6 files changed, 39 insertions, 23 deletions
diff --git a/main/ccss.c b/main/ccss.c
index 6ce022ddf..b479a3c54 100644
--- a/main/ccss.c
+++ b/main/ccss.c
@@ -649,7 +649,7 @@ static void ccss_notify_device_state_change(const char *device, enum cc_state st
"Notification of CCSS state change to '%s', device state '%s' for device '%s'\n",
cc_state_to_string(state), ast_devstate2str(devstate), device);
- ast_devstate_changed(devstate, "ccss:%s", device);
+ ast_devstate_changed(devstate, AST_DEVSTATE_CACHABLE, "ccss:%s", device);
}
#define CC_OFFER_TIMER_DEFAULT 20 /* Seconds */
diff --git a/main/channel.c b/main/channel.c
index 38d11b1c0..d1d4d095d 100644
--- a/main/channel.c
+++ b/main/channel.c
@@ -2482,7 +2482,7 @@ static void ast_channel_destructor(void *obj)
* instance is dead, we don't know the state of all other possible
* instances.
*/
- ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, device_name);
+ ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE) ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), device_name);
}
ast_channel_nativeformats_set(chan, ast_format_cap_destroy(ast_channel_nativeformats(chan)));
@@ -7401,7 +7401,7 @@ int ast_setstate(struct ast_channel *chan, enum ast_channel_state state)
/* We have to pass AST_DEVICE_UNKNOWN here because it is entirely possible that the channel driver
* for this channel is using the callback method for device state. If we pass in an actual state here
* we override what they are saying the state is and things go amuck. */
- ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, name);
+ ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE) ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), name);
/* setstate used to conditionally report Newchannel; this is no more */
/*** DOCUMENTATION
diff --git a/main/channel_internal_api.c b/main/channel_internal_api.c
index c7ed84c34..3f892ddef 100644
--- a/main/channel_internal_api.c
+++ b/main/channel_internal_api.c
@@ -263,6 +263,7 @@ static void channel_data_add_flags(struct ast_data *tree,
ast_data_add_bool(tree, "BRIDGE_HANGUP_RUN", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_HANGUP_RUN));
ast_data_add_bool(tree, "BRIDGE_HANGUP_DONT", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_HANGUP_DONT));
ast_data_add_bool(tree, "DISABLE_WORKAROUNDS", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_WORKAROUNDS));
+ ast_data_add_bool(tree, "DISABLE_DEVSTATE_CACHE", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE));
}
int ast_channel_data_add_structure(struct ast_data *tree,
diff --git a/main/devicestate.c b/main/devicestate.c
index 3256a7b4f..4ed51f9e3 100644
--- a/main/devicestate.c
+++ b/main/devicestate.c
@@ -174,6 +174,7 @@ static AST_RWLIST_HEAD_STATIC(devstate_provs, devstate_prov);
struct state_change {
AST_LIST_ENTRY(state_change) list;
+ enum ast_devstate_cache cachable;
char device[1];
};
@@ -191,6 +192,7 @@ struct devstate_change {
AST_LIST_ENTRY(devstate_change) entry;
uint32_t state;
struct ast_eid eid;
+ enum ast_devstate_cache cachable;
char device[1];
};
@@ -424,7 +426,7 @@ static int getproviderstate(const char *provider, const char *address)
return res;
}
-static void devstate_event(const char *device, enum ast_device_state state)
+static void devstate_event(const char *device, enum ast_device_state state, int cachable)
{
struct ast_event *event;
enum ast_event_type event_type;
@@ -440,18 +442,23 @@ static void devstate_event(const char *device, enum ast_device_state state)
ast_debug(3, "device '%s' state '%d'\n", device, state);
if (!(event = ast_event_new(event_type,
- AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
- AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
- AST_EVENT_IE_END))) {
+ AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
+ AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
+ AST_EVENT_IE_CACHABLE, AST_EVENT_IE_PLTYPE_UINT, cachable,
+ AST_EVENT_IE_END))) {
return;
}
- ast_event_queue_and_cache(event);
+ if (cachable) {
+ ast_event_queue_and_cache(event);
+ } else {
+ ast_event_queue(event);
+ }
}
/*! Called by the state change thread to find out what the state is, and then
* to queue up the state change event */
-static void do_state_change(const char *device)
+static void do_state_change(const char *device, int cachable)
{
enum ast_device_state state;
@@ -459,10 +466,10 @@ static void do_state_change(const char *device)
ast_debug(3, "Changing state for %s - state %d (%s)\n", device, state, ast_devstate2str(state));
- devstate_event(device, state);
+ devstate_event(device, state, cachable);
}
-int ast_devstate_changed_literal(enum ast_device_state state, const char *device)
+int ast_devstate_changed_literal(enum ast_device_state state, enum ast_devstate_cache cachable, const char *device)
{
struct state_change *change;
@@ -483,14 +490,15 @@ int ast_devstate_changed_literal(enum ast_device_state state, const char *device
*/
if (state != AST_DEVICE_UNKNOWN) {
- devstate_event(device, state);
+ devstate_event(device, state, cachable);
} else if (change_thread == AST_PTHREADT_NULL || !(change = ast_calloc(1, sizeof(*change) + strlen(device)))) {
/* we could not allocate a change struct, or */
/* there is no background thread, so process the change now */
- do_state_change(device);
+ do_state_change(device, cachable);
} else {
/* queue the change */
strcpy(change->device, device);
+ change->cachable = cachable;
AST_LIST_LOCK(&state_changes);
AST_LIST_INSERT_TAIL(&state_changes, change, list);
ast_cond_signal(&change_pending);
@@ -502,10 +510,10 @@ int ast_devstate_changed_literal(enum ast_device_state state, const char *device
int ast_device_state_changed_literal(const char *dev)
{
- return ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, dev);
+ return ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, dev);
}
-int ast_devstate_changed(enum ast_device_state state, const char *fmt, ...)
+int ast_devstate_changed(enum ast_device_state state, enum ast_devstate_cache cachable, const char *fmt, ...)
{
char buf[AST_MAX_EXTENSION];
va_list ap;
@@ -514,7 +522,7 @@ int ast_devstate_changed(enum ast_device_state state, const char *fmt, ...)
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
- return ast_devstate_changed_literal(state, buf);
+ return ast_devstate_changed_literal(state, cachable, buf);
}
int ast_device_state_changed(const char *fmt, ...)
@@ -526,7 +534,7 @@ int ast_device_state_changed(const char *fmt, ...)
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
- return ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, buf);
+ return ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, buf);
}
/*! \brief Go through the dev state change queue and update changes in the dev state thread */
@@ -546,7 +554,7 @@ static void *do_devstate_changes(void *data)
/* Process each state change */
while ((current = next)) {
next = AST_LIST_NEXT(current, list);
- do_state_change(current->device);
+ do_state_change(current->device, current->cachable);
ast_free(current);
}
}
@@ -590,7 +598,7 @@ static void devstate_cache_cb(const struct ast_event *event, void *data)
collection->num_states++;
}
-static void process_collection(const char *device, struct change_collection *collection)
+static void process_collection(const char *device, enum ast_devstate_cache cachable, struct change_collection *collection)
{
int i;
struct ast_devstate_aggregate agg;
@@ -641,7 +649,11 @@ static void process_collection(const char *device, struct change_collection *col
return;
}
- ast_event_queue_and_cache(event);
+ if (cachable) {
+ ast_event_queue_and_cache(event);
+ } else {
+ ast_event_queue(event);
+ }
}
static void handle_devstate_change(struct devstate_change *sc)
@@ -667,7 +679,7 @@ static void handle_devstate_change(struct devstate_change *sc)
/* Populate the collection of device states from the cache */
ast_event_dump_cache(tmp_sub);
- process_collection(sc->device, &collection);
+ process_collection(sc->device, sc->cachable, &collection);
ast_event_sub_destroy(tmp_sub);
}
@@ -696,10 +708,12 @@ static void devstate_change_collector_cb(const struct ast_event *event, void *da
const char *device;
const struct ast_eid *eid;
uint32_t state;
+ enum ast_devstate_cache cachable = AST_DEVSTATE_CACHABLE;
device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE);
eid = ast_event_get_ie_raw(event, AST_EVENT_IE_EID);
state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
+ cachable = ast_event_get_ie_uint(event, AST_EVENT_IE_CACHABLE);
if (ast_strlen_zero(device) || !eid) {
ast_log(LOG_ERROR, "Invalid device state change event received\n");
@@ -712,6 +726,7 @@ static void devstate_change_collector_cb(const struct ast_event *event, void *da
strcpy(sc->device, device);
sc->eid = *eid;
sc->state = state;
+ sc->cachable = cachable;
ast_mutex_lock(&devstate_collector.lock);
AST_LIST_INSERT_TAIL(&devstate_collector.devstate_change_q, sc, entry);
diff --git a/main/event.c b/main/event.c
index 2c4670b52..34ba01942 100644
--- a/main/event.c
+++ b/main/event.c
@@ -279,7 +279,7 @@ static const struct ie_map {
[AST_EVENT_IE_RECEIVED_HASH] = { AST_EVENT_IE_PLTYPE_STR, "ReceivedHash" },
[AST_EVENT_IE_USING_PASSWORD] = { AST_EVENT_IE_PLTYPE_UINT, "UsingPassword" },
[AST_EVENT_IE_ATTEMPTED_TRANSPORT] = { AST_EVENT_IE_PLTYPE_STR, "AttemptedTransport" },
-
+ [AST_EVENT_IE_CACHABLE] = { AST_EVENT_IE_PLTYPE_UINT, "Cachable" },
};
const char *ast_event_get_type_name(const struct ast_event *event)
diff --git a/main/features.c b/main/features.c
index 5e544cc26..44f140e16 100644
--- a/main/features.c
+++ b/main/features.c
@@ -1257,7 +1257,7 @@ static void notify_metermaids(const char *exten, char *context, enum ast_device_
ast_debug(4, "Notification of state change to metermaids %s@%s\n to state '%s'",
exten, context, ast_devstate2str(state));
- ast_devstate_changed(state, "park:%s@%s", exten, context);
+ ast_devstate_changed(state, AST_DEVSTATE_CACHABLE, "park:%s@%s", exten, context);
}
/*! \brief metermaids callback from devicestate.c */