blob: 28fba6cf50f588290e3a740bd982026044422e66 [file] [log] [blame]
Patrick Williamsb48b7b42016-08-17 15:04:38 -05001Avoid assigning thread data outside of mutex lock
2
3Patch originally from OpenSUSE:
4https://build.opensuse.org/package/show?package=atftp&project=openSUSE%3A12.2
5
6Upstream-Status: Pending
7
8Index: git/tftpd_list.c
9===================================================================
10--- git.orig/tftpd_list.c 2012-10-24 21:48:47.000000000 -0700
11+++ git/tftpd_list.c 2012-10-24 21:52:04.266205076 -0700
12@@ -49,11 +49,11 @@
13 */
14 int tftpd_list_add(struct thread_data *new)
15 {
16- struct thread_data *current = thread_data;
17+ struct thread_data *current;
18 int ret;
19
20 pthread_mutex_lock(&thread_list_mutex);
21-
22+ current = thread_data;
23 number_of_thread++;
24
25 ret = number_of_thread;
26@@ -81,11 +81,13 @@
27 */
28 int tftpd_list_remove(struct thread_data *old)
29 {
30- struct thread_data *current = thread_data;
31+ struct thread_data *current;
32 int ret;
33
34 pthread_mutex_lock(&thread_list_mutex);
35
36+ current = thread_data;
37+
38 number_of_thread--;
39 ret = number_of_thread;
40
41@@ -137,23 +139,26 @@
42 struct thread_data *data,
43 struct client_info *client)
44 {
45- struct thread_data *current = thread_data; /* head of the list */
46- struct tftp_opt *tftp_options = data->tftp_options;
47+ struct thread_data *current; /* head of the list */
48+ struct tftp_opt *tftp_options;
49 struct client_info *tmp;
50 char options[MAXLEN];
51 char string[MAXLEN];
52 char *index;
53 int len;
54
55+ /* lock the whole list before walking it */
56+ pthread_mutex_lock(&thread_list_mutex);
57+
58 *thread = NULL;
59
60+ current = thread_data;
61+ tftp_options = data->tftp_options;
62+
63 opt_request_to_string(tftp_options, options, MAXLEN);
64 index = strstr(options, "multicast");
65 len = (int)index - (int)options;
66
67- /* lock the whole list before walking it */
68- pthread_mutex_lock(&thread_list_mutex);
69-
70 while (current)
71 {
72 if (current != data)
73@@ -214,9 +219,10 @@
74 void tftpd_clientlist_remove(struct thread_data *thread,
75 struct client_info *client)
76 {
77- struct client_info *tmp = thread->client_info;
78+ struct client_info *tmp;
79
80 pthread_mutex_lock(&thread->client_mutex);
81+ tmp = thread->client_info;
82 while ((tmp->next != client) && (tmp->next != NULL))
83 tmp = tmp->next;
84 if (tmp->next == NULL)
85@@ -231,9 +237,11 @@
86 void tftpd_clientlist_free(struct thread_data *thread)
87 {
88 struct client_info *tmp;
89- struct client_info *head = thread->client_info;
90+ struct client_info *head;
91
92 pthread_mutex_lock(&thread->client_mutex);
93+ head = thread->client_info;
94+
95 while (head)
96 {
97 tmp = head;
98@@ -250,9 +258,10 @@
99 struct client_info *client,
100 struct sockaddr_storage *sock)
101 {
102- struct client_info *head = thread->client_info;
103+ struct client_info *head;
104
105 pthread_mutex_lock(&thread->client_mutex);
106+ head = thread->client_info;
107
108 if (client)
109 {
110@@ -334,10 +343,10 @@
111
112 void tftpd_list_kill_threads(void)
113 {
114- struct thread_data *current = thread_data; /* head of list */
115+ struct thread_data *current; /* head of list */
116
117 pthread_mutex_lock(&thread_list_mutex);
118-
119+ current = thread_data;
120
121 while (current != NULL)
122 {
123Index: git/tftpd_mcast.c
124===================================================================
125--- git.orig/tftpd_mcast.c 2012-10-24 21:48:47.000000000 -0700
126+++ git/tftpd_mcast.c 2012-10-24 21:49:11.570201582 -0700
127@@ -51,9 +51,11 @@
128 */
129 int tftpd_mcast_get_tid(char **addr, short *port)
130 {
131- struct tid *current = tid_list;
132+ struct tid *current;
133
134 pthread_mutex_lock(&mcast_tid_list);
135+ current = tid_list;
136+
137 /* walk the list for a free tid */
138 while (current != NULL)
139 {
140@@ -74,9 +76,11 @@
141
142 int tftpd_mcast_free_tid(char *addr, short port)
143 {
144- struct tid *current = tid_list;
145+ struct tid *current;
146
147 pthread_mutex_lock(&mcast_tid_list);
148+ current = tid_list;
149+
150 while (current != NULL)
151 {
152 if ((current->used == 1) && (current->port == port) &&