ALSA: seq: Fix possible UAF in snd_seq_check_queue()
[pandora-kernel.git] / sound / core / seq / seq_prioq.c
1 /*
2  *   ALSA sequencer Priority Queue
3  *   Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
4  *
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21
22 #include <linux/time.h>
23 #include <linux/slab.h>
24 #include <sound/core.h>
25 #include "seq_timer.h"
26 #include "seq_prioq.h"
27
28
29 /* Implementation is a simple linked list for now...
30
31    This priority queue orders the events on timestamp. For events with an
32    equeal timestamp the queue behaves as a FIFO. 
33
34    *
35    *           +-------+
36    *  Head --> | first |
37    *           +-------+
38    *                 |next
39    *           +-----v-+
40    *           |       |
41    *           +-------+
42    *                 |
43    *           +-----v-+
44    *           |       |
45    *           +-------+
46    *                 |
47    *           +-----v-+
48    *  Tail --> | last  |
49    *           +-------+
50    *
51
52  */
53
54
55
56 /* create new prioq (constructor) */
57 struct snd_seq_prioq *snd_seq_prioq_new(void)
58 {
59         struct snd_seq_prioq *f;
60
61         f = kzalloc(sizeof(*f), GFP_KERNEL);
62         if (f == NULL) {
63                 snd_printd("oops: malloc failed for snd_seq_prioq_new()\n");
64                 return NULL;
65         }
66         
67         spin_lock_init(&f->lock);
68         f->head = NULL;
69         f->tail = NULL;
70         f->cells = 0;
71         
72         return f;
73 }
74
75 /* delete prioq (destructor) */
76 void snd_seq_prioq_delete(struct snd_seq_prioq **fifo)
77 {
78         struct snd_seq_prioq *f = *fifo;
79         *fifo = NULL;
80
81         if (f == NULL) {
82                 snd_printd("oops: snd_seq_prioq_delete() called with NULL prioq\n");
83                 return;
84         }
85
86         /* release resources...*/
87         /*....................*/
88         
89         if (f->cells > 0) {
90                 /* drain prioQ */
91                 while (f->cells > 0)
92                         snd_seq_cell_free(snd_seq_prioq_cell_out(f, NULL));
93         }
94         
95         kfree(f);
96 }
97
98
99
100
101 /* compare timestamp between events */
102 /* return 1 if a >= b; 0 */
103 static inline int compare_timestamp(struct snd_seq_event *a,
104                                     struct snd_seq_event *b)
105 {
106         if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
107                 /* compare ticks */
108                 return (snd_seq_compare_tick_time(&a->time.tick, &b->time.tick));
109         } else {
110                 /* compare real time */
111                 return (snd_seq_compare_real_time(&a->time.time, &b->time.time));
112         }
113 }
114
115 /* compare timestamp between events */
116 /* return negative if a < b;
117  *        zero     if a = b;
118  *        positive if a > b;
119  */
120 static inline int compare_timestamp_rel(struct snd_seq_event *a,
121                                         struct snd_seq_event *b)
122 {
123         if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
124                 /* compare ticks */
125                 if (a->time.tick > b->time.tick)
126                         return 1;
127                 else if (a->time.tick == b->time.tick)
128                         return 0;
129                 else
130                         return -1;
131         } else {
132                 /* compare real time */
133                 if (a->time.time.tv_sec > b->time.time.tv_sec)
134                         return 1;
135                 else if (a->time.time.tv_sec == b->time.time.tv_sec) {
136                         if (a->time.time.tv_nsec > b->time.time.tv_nsec)
137                                 return 1;
138                         else if (a->time.time.tv_nsec == b->time.time.tv_nsec)
139                                 return 0;
140                         else
141                                 return -1;
142                 } else
143                         return -1;
144         }
145 }
146
147 /* enqueue cell to prioq */
148 int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
149                           struct snd_seq_event_cell * cell)
150 {
151         struct snd_seq_event_cell *cur, *prev;
152         unsigned long flags;
153         int count;
154         int prior;
155
156         if (snd_BUG_ON(!f || !cell))
157                 return -EINVAL;
158         
159         /* check flags */
160         prior = (cell->event.flags & SNDRV_SEQ_PRIORITY_MASK);
161
162         spin_lock_irqsave(&f->lock, flags);
163
164         /* check if this element needs to inserted at the end (ie. ordered 
165            data is inserted) This will be very likeley if a sequencer 
166            application or midi file player is feeding us (sequential) data */
167         if (f->tail && !prior) {
168                 if (compare_timestamp(&cell->event, &f->tail->event)) {
169                         /* add new cell to tail of the fifo */
170                         f->tail->next = cell;
171                         f->tail = cell;
172                         cell->next = NULL;
173                         f->cells++;
174                         spin_unlock_irqrestore(&f->lock, flags);
175                         return 0;
176                 }
177         }
178         /* traverse list of elements to find the place where the new cell is
179            to be inserted... Note that this is a order n process ! */
180
181         prev = NULL;            /* previous cell */
182         cur = f->head;          /* cursor */
183
184         count = 10000; /* FIXME: enough big, isn't it? */
185         while (cur != NULL) {
186                 /* compare timestamps */
187                 int rel = compare_timestamp_rel(&cell->event, &cur->event);
188                 if (rel < 0)
189                         /* new cell has earlier schedule time, */
190                         break;
191                 else if (rel == 0 && prior)
192                         /* equal schedule time and prior to others */
193                         break;
194                 /* new cell has equal or larger schedule time, */
195                 /* move cursor to next cell */
196                 prev = cur;
197                 cur = cur->next;
198                 if (! --count) {
199                         spin_unlock_irqrestore(&f->lock, flags);
200                         snd_printk(KERN_ERR "cannot find a pointer.. infinite loop?\n");
201                         return -EINVAL;
202                 }
203         }
204
205         /* insert it before cursor */
206         if (prev != NULL)
207                 prev->next = cell;
208         cell->next = cur;
209
210         if (f->head == cur) /* this is the first cell, set head to it */
211                 f->head = cell;
212         if (cur == NULL) /* reached end of the list */
213                 f->tail = cell;
214         f->cells++;
215         spin_unlock_irqrestore(&f->lock, flags);
216         return 0;
217 }
218
219 /* return 1 if the current time >= event timestamp */
220 static int event_is_ready(struct snd_seq_event *ev, void *current_time)
221 {
222         if ((ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK)
223                 return snd_seq_compare_tick_time(current_time, &ev->time.tick);
224         else
225                 return snd_seq_compare_real_time(current_time, &ev->time.time);
226 }
227
228 /* dequeue cell from prioq */
229 struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f,
230                                                   void *current_time)
231 {
232         struct snd_seq_event_cell *cell;
233         unsigned long flags;
234
235         if (f == NULL) {
236                 snd_printd("oops: snd_seq_prioq_cell_in() called with NULL prioq\n");
237                 return NULL;
238         }
239         spin_lock_irqsave(&f->lock, flags);
240
241         cell = f->head;
242         if (cell && current_time && !event_is_ready(&cell->event, current_time))
243                 cell = NULL;
244         if (cell) {
245                 f->head = cell->next;
246
247                 /* reset tail if this was the last element */
248                 if (f->tail == cell)
249                         f->tail = NULL;
250
251                 cell->next = NULL;
252                 f->cells--;
253         }
254
255         spin_unlock_irqrestore(&f->lock, flags);
256         return cell;
257 }
258
259 /* return number of events available in prioq */
260 int snd_seq_prioq_avail(struct snd_seq_prioq * f)
261 {
262         if (f == NULL) {
263                 snd_printd("oops: snd_seq_prioq_cell_in() called with NULL prioq\n");
264                 return 0;
265         }
266         return f->cells;
267 }
268
269
270 static inline int prioq_match(struct snd_seq_event_cell *cell,
271                               int client, int timestamp)
272 {
273         if (cell->event.source.client == client ||
274             cell->event.dest.client == client)
275                 return 1;
276         if (!timestamp)
277                 return 0;
278         switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) {
279         case SNDRV_SEQ_TIME_STAMP_TICK:
280                 if (cell->event.time.tick)
281                         return 1;
282                 break;
283         case SNDRV_SEQ_TIME_STAMP_REAL:
284                 if (cell->event.time.time.tv_sec ||
285                     cell->event.time.time.tv_nsec)
286                         return 1;
287                 break;
288         }
289         return 0;
290 }
291
292 /* remove cells for left client */
293 void snd_seq_prioq_leave(struct snd_seq_prioq * f, int client, int timestamp)
294 {
295         register struct snd_seq_event_cell *cell, *next;
296         unsigned long flags;
297         struct snd_seq_event_cell *prev = NULL;
298         struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext;
299
300         /* collect all removed cells */
301         spin_lock_irqsave(&f->lock, flags);
302         cell = f->head;
303         while (cell) {
304                 next = cell->next;
305                 if (prioq_match(cell, client, timestamp)) {
306                         /* remove cell from prioq */
307                         if (cell == f->head) {
308                                 f->head = cell->next;
309                         } else {
310                                 prev->next = cell->next;
311                         }
312                         if (cell == f->tail)
313                                 f->tail = cell->next;
314                         f->cells--;
315                         /* add cell to free list */
316                         cell->next = NULL;
317                         if (freefirst == NULL) {
318                                 freefirst = cell;
319                         } else {
320                                 freeprev->next = cell;
321                         }
322                         freeprev = cell;
323                 } else {
324 #if 0
325                         printk(KERN_DEBUG "type = %i, source = %i, dest = %i, "
326                                "client = %i\n",
327                                 cell->event.type,
328                                 cell->event.source.client,
329                                 cell->event.dest.client,
330                                 client);
331 #endif
332                         prev = cell;
333                 }
334                 cell = next;            
335         }
336         spin_unlock_irqrestore(&f->lock, flags);        
337
338         /* remove selected cells */
339         while (freefirst) {
340                 freenext = freefirst->next;
341                 snd_seq_cell_free(freefirst);
342                 freefirst = freenext;
343         }
344 }
345
346 static int prioq_remove_match(struct snd_seq_remove_events *info,
347                               struct snd_seq_event *ev)
348 {
349         int res;
350
351         if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST) {
352                 if (ev->dest.client != info->dest.client ||
353                                 ev->dest.port != info->dest.port)
354                         return 0;
355         }
356         if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST_CHANNEL) {
357                 if (! snd_seq_ev_is_channel_type(ev))
358                         return 0;
359                 /* data.note.channel and data.control.channel are identical */
360                 if (ev->data.note.channel != info->channel)
361                         return 0;
362         }
363         if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_AFTER) {
364                 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
365                         res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick);
366                 else
367                         res = snd_seq_compare_real_time(&ev->time.time, &info->time.time);
368                 if (!res)
369                         return 0;
370         }
371         if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_BEFORE) {
372                 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
373                         res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick);
374                 else
375                         res = snd_seq_compare_real_time(&ev->time.time, &info->time.time);
376                 if (res)
377                         return 0;
378         }
379         if (info->remove_mode & SNDRV_SEQ_REMOVE_EVENT_TYPE) {
380                 if (ev->type != info->type)
381                         return 0;
382         }
383         if (info->remove_mode & SNDRV_SEQ_REMOVE_IGNORE_OFF) {
384                 /* Do not remove off events */
385                 switch (ev->type) {
386                 case SNDRV_SEQ_EVENT_NOTEOFF:
387                 /* case SNDRV_SEQ_EVENT_SAMPLE_STOP: */
388                         return 0;
389                 default:
390                         break;
391                 }
392         }
393         if (info->remove_mode & SNDRV_SEQ_REMOVE_TAG_MATCH) {
394                 if (info->tag != ev->tag)
395                         return 0;
396         }
397
398         return 1;
399 }
400
401 /* remove cells matching remove criteria */
402 void snd_seq_prioq_remove_events(struct snd_seq_prioq * f, int client,
403                                  struct snd_seq_remove_events *info)
404 {
405         struct snd_seq_event_cell *cell, *next;
406         unsigned long flags;
407         struct snd_seq_event_cell *prev = NULL;
408         struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext;
409
410         /* collect all removed cells */
411         spin_lock_irqsave(&f->lock, flags);
412         cell = f->head;
413
414         while (cell) {
415                 next = cell->next;
416                 if (cell->event.source.client == client &&
417                         prioq_remove_match(info, &cell->event)) {
418
419                         /* remove cell from prioq */
420                         if (cell == f->head) {
421                                 f->head = cell->next;
422                         } else {
423                                 prev->next = cell->next;
424                         }
425
426                         if (cell == f->tail)
427                                 f->tail = cell->next;
428                         f->cells--;
429
430                         /* add cell to free list */
431                         cell->next = NULL;
432                         if (freefirst == NULL) {
433                                 freefirst = cell;
434                         } else {
435                                 freeprev->next = cell;
436                         }
437
438                         freeprev = cell;
439                 } else {
440                         prev = cell;
441                 }
442                 cell = next;            
443         }
444         spin_unlock_irqrestore(&f->lock, flags);        
445
446         /* remove selected cells */
447         while (freefirst) {
448                 freenext = freefirst->next;
449                 snd_seq_cell_free(freefirst);
450                 freefirst = freenext;
451         }
452 }
453
454