Bläddra i källkod

Add a check field to the handle structure to make it less
likely to get a random valid handle.
Fix a couple bugs in the event service that this change exposed.


git-svn-id: http://svn.fedorahosted.org/svn/corosync/trunk@791 fd59a12c-fef9-0310-b244-a6a79926bd2f

Mark Haverkamp 20 år sedan
förälder
incheckning
fbddefe965
3 ändrade filer med 50 tillägg och 15 borttagningar
  1. 3 3
      exec/evt.c
  2. 4 4
      lib/evt.c
  3. 43 8
      lib/util.c

+ 3 - 3
exec/evt.c

@@ -314,7 +314,7 @@ struct open_chan_pending {
 	struct conn_info	*ocp_conn_info;
 	struct conn_info	*ocp_conn_info;
 	SaEvtChannelOpenFlagsT	ocp_open_flag;
 	SaEvtChannelOpenFlagsT	ocp_open_flag;
 	poll_timer_handle	ocp_timer_handle;
 	poll_timer_handle	ocp_timer_handle;
-	uint32_t			ocp_c_handle;
+	uint64_t			ocp_c_handle;
 	struct list_head	ocp_entry;
 	struct list_head	ocp_entry;
 };
 };
 
 
@@ -491,7 +491,7 @@ struct event_data {
  * 					(struct event_server_instance.esi_events)
  * 					(struct event_server_instance.esi_events)
  */
  */
 struct chan_event_list {
 struct chan_event_list {
-	uint32_t			cel_chan_handle;
+	uint64_t			cel_chan_handle;
 	uint32_t			cel_sub_id;
 	uint32_t			cel_sub_id;
 	struct event_data* 	cel_event;
 	struct event_data* 	cel_event;
 	struct list_head 	cel_entry;
 	struct list_head 	cel_entry;
@@ -513,7 +513,7 @@ struct chan_event_list {
  */
  */
 struct event_svr_channel_open {
 struct event_svr_channel_open {
 	uint8_t								eco_flags;
 	uint8_t								eco_flags;
-	uint32_t							eco_lib_handle;
+	uint64_t							eco_lib_handle;
 	uint32_t							eco_my_handle;
 	uint32_t							eco_my_handle;
 	struct event_svr_channel_instance 	*eco_channel;
 	struct event_svr_channel_instance 	*eco_channel;
 	struct list_head 					eco_entry;
 	struct list_head 					eco_entry;

+ 4 - 4
lib/evt.c

@@ -354,7 +354,7 @@ saEvtInitialize(
 	 * assign instance data to unique handle
 	 * assign instance data to unique handle
 	 */
 	 */
 	error = saHandleCreate(&evt_instance_handle_db, sizeof(*evti), 
 	error = saHandleCreate(&evt_instance_handle_db, sizeof(*evti), 
-			(void*)evtHandle);
+			evtHandle);
 	if (error != SA_AIS_OK) {
 	if (error != SA_AIS_OK) {
 		goto error_nofree;
 		goto error_nofree;
 	}
 	}
@@ -460,7 +460,7 @@ static SaAisErrorT make_event(SaEvtEventHandleT *event_handle,
 	int i;
 	int i;
 
 
 	error = saHandleCreate(&event_handle_db, sizeof(*edi), 
 	error = saHandleCreate(&event_handle_db, sizeof(*edi), 
-		(void*)event_handle);
+		event_handle);
 	if (error != SA_AIS_OK) {
 	if (error != SA_AIS_OK) {
 		if (error == SA_AIS_ERR_NO_MEMORY) {
 		if (error == SA_AIS_ERR_NO_MEMORY) {
 			error = SA_AIS_ERR_LIBRARY;
 			error = SA_AIS_ERR_LIBRARY;
@@ -904,7 +904,7 @@ saEvtChannelOpen(
 	 * create a handle for this open channel
 	 * create a handle for this open channel
 	 */
 	 */
 	error = saHandleCreate(&channel_handle_db, sizeof(*eci), 
 	error = saHandleCreate(&channel_handle_db, sizeof(*eci), 
-			(void*)channelHandle);
+			channelHandle);
 	if (error != SA_AIS_OK) {
 	if (error != SA_AIS_OK) {
 		goto chan_open_put;
 		goto chan_open_put;
 	}
 	}
@@ -1309,7 +1309,7 @@ saEvtEventAllocate(
 	}
 	}
 
 
 	error = saHandleCreate(&event_handle_db, sizeof(*edi), 
 	error = saHandleCreate(&event_handle_db, sizeof(*edi), 
-			(void*)eventHandle);
+			eventHandle);
 	if (error != SA_AIS_OK) {
 	if (error != SA_AIS_OK) {
 		goto alloc_put2;
 		goto alloc_put2;
 	}
 	}

+ 43 - 8
lib/util.c

@@ -64,6 +64,7 @@ struct saHandle {
 	int state;
 	int state;
 	void *instance;
 	void *instance;
 	int refCount;
 	int refCount;
+	uint32_t check;
 };
 };
 
 
 SaErrorT
 SaErrorT
@@ -425,7 +426,8 @@ saHandleCreate (
 	int instanceSize,
 	int instanceSize,
 	SaUint64T *handleOut)
 	SaUint64T *handleOut)
 {
 {
-	int handle;
+	uint32_t handle;
+	uint32_t check;
 	void *newHandles;
 	void *newHandles;
 	int found = 0;
 	int found = 0;
 	void *instance;
 	void *instance;
@@ -454,6 +456,10 @@ saHandleCreate (
 	if (instance == 0) {
 	if (instance == 0) {
 		return (SA_AIS_ERR_NO_MEMORY);
 		return (SA_AIS_ERR_NO_MEMORY);
 	}
 	}
+
+
+	check = random();
+
 	memset (instance, 0, instanceSize);
 	memset (instance, 0, instanceSize);
 
 
 	handleDatabase->handles[handle].state = SA_HANDLE_STATE_ACTIVE;
 	handleDatabase->handles[handle].state = SA_HANDLE_STATE_ACTIVE;
@@ -462,7 +468,9 @@ saHandleCreate (
 
 
 	handleDatabase->handles[handle].refCount = 1;
 	handleDatabase->handles[handle].refCount = 1;
 
 
-	*handleOut = handle;
+	handleDatabase->handles[handle].check = check;
+
+	*handleOut = (SaUint64T)((uint64_t)check << 32 | handle);
 
 
 	pthread_mutex_unlock (&handleDatabase->mutex);
 	pthread_mutex_unlock (&handleDatabase->mutex);
 
 
@@ -473,26 +481,39 @@ saHandleCreate (
 SaErrorT
 SaErrorT
 saHandleDestroy (
 saHandleDestroy (
 	struct saHandleDatabase *handleDatabase,
 	struct saHandleDatabase *handleDatabase,
-	SaUint64T handle)
+	SaUint64T inHandle)
 {
 {
+	SaAisErrorT error = SA_AIS_OK;
+	uint32_t check = inHandle >> 32;
+	uint32_t handle = inHandle & 0xffffffff;
+
 	pthread_mutex_lock (&handleDatabase->mutex);
 	pthread_mutex_lock (&handleDatabase->mutex);
 
 
+	if (check != handleDatabase->handles[handle].check) {
+		error = SA_AIS_ERR_BAD_HANDLE;
+		goto error_exit;
+	}
+
 	handleDatabase->handles[handle].state = SA_HANDLE_STATE_PENDINGREMOVAL;
 	handleDatabase->handles[handle].state = SA_HANDLE_STATE_PENDINGREMOVAL;
 
 
+error_exit:
 	pthread_mutex_unlock (&handleDatabase->mutex);
 	pthread_mutex_unlock (&handleDatabase->mutex);
 
 
-	saHandleInstancePut (handleDatabase, handle);
+	saHandleInstancePut (handleDatabase, inHandle);
 
 
-	return (SA_AIS_OK);
+	return (error);
 }
 }
 
 
 
 
 SaErrorT
 SaErrorT
 saHandleInstanceGet (
 saHandleInstanceGet (
 	struct saHandleDatabase *handleDatabase,
 	struct saHandleDatabase *handleDatabase,
-	SaUint64T handle,
+	SaUint64T inHandle,
 	void **instance)
 	void **instance)
 { 
 { 
+	uint32_t check = inHandle >> 32;
+	uint32_t handle = inHandle & 0xffffffff;
+
 	SaErrorT error = SA_AIS_OK;
 	SaErrorT error = SA_AIS_OK;
 	pthread_mutex_lock (&handleDatabase->mutex);
 	pthread_mutex_lock (&handleDatabase->mutex);
 
 
@@ -504,6 +525,11 @@ saHandleInstanceGet (
 		error = SA_AIS_ERR_BAD_HANDLE;
 		error = SA_AIS_ERR_BAD_HANDLE;
 		goto error_exit;
 		goto error_exit;
 	}
 	}
+	if (check != handleDatabase->handles[handle].check) {
+		error = SA_AIS_ERR_BAD_HANDLE;
+		goto error_exit;
+	}
+
 
 
 	*instance = handleDatabase->handles[handle].instance;
 	*instance = handleDatabase->handles[handle].instance;
 
 
@@ -519,12 +545,20 @@ error_exit:
 SaErrorT
 SaErrorT
 saHandleInstancePut (
 saHandleInstancePut (
 	struct saHandleDatabase *handleDatabase,
 	struct saHandleDatabase *handleDatabase,
-	SaUint64T handle)
+	SaUint64T inHandle)
 {
 {
 	void *instance;
 	void *instance;
+	SaAisErrorT error = SA_AIS_OK;
+	uint32_t check = inHandle >> 32;
+	uint32_t handle = inHandle & 0xffffffff;
 
 
 	pthread_mutex_lock (&handleDatabase->mutex);
 	pthread_mutex_lock (&handleDatabase->mutex);
 
 
+	if (check != handleDatabase->handles[handle].check) {
+		error = SA_AIS_ERR_BAD_HANDLE;
+		goto error_exit;
+	}
+
 	handleDatabase->handles[handle].refCount -= 1;
 	handleDatabase->handles[handle].refCount -= 1;
 	assert (handleDatabase->handles[handle].refCount >= 0);
 	assert (handleDatabase->handles[handle].refCount >= 0);
 
 
@@ -535,9 +569,10 @@ saHandleInstancePut (
 		memset (&handleDatabase->handles[handle], 0, sizeof (struct saHandle));
 		memset (&handleDatabase->handles[handle], 0, sizeof (struct saHandle));
 	}
 	}
 
 
+error_exit:
 	pthread_mutex_unlock (&handleDatabase->mutex);
 	pthread_mutex_unlock (&handleDatabase->mutex);
 
 
-	return (SA_AIS_OK);
+	return (error);
 }
 }