blob: 9223823b4364ce1141464f959f482a74fcb84df1 [file] [log] [blame]
Eric Laurent4dacbc32020-10-07 13:48:21 -07001/*
2 * Copyright (C) 2020 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#include <media/AudioSanitizer.h>
18
19namespace android {
20
21 /** returns true if string overflow was prevented by zero termination */
22template <size_t size>
23bool preventStringOverflow(char (&s)[size]) {
24 if (strnlen(s, size) < size) return false;
25 s[size - 1] = '\0';
26 return true;
27}
28
29status_t safetyNetLog(status_t status, const char *bugNumber) {
30 if (status != NO_ERROR && bugNumber != nullptr) {
31 android_errorWriteLog(0x534e4554, bugNumber); // SafetyNet logging
32 }
33 return status;
34}
35
36status_t AudioSanitizer::sanitizeAudioAttributes(
37 audio_attributes_t *attr, const char *bugNumber)
38{
39 status_t status = NO_ERROR;
40 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
41 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
42 status = BAD_VALUE;
43 }
44 attr->tags[tagsMaxSize - 1] = '\0';
45 return safetyNetLog(status, bugNumber);
46}
47
48/** returns BAD_VALUE if sanitization was required. */
49status_t AudioSanitizer::sanitizeEffectDescriptor(
50 effect_descriptor_t *desc, const char *bugNumber)
51{
52 status_t status = NO_ERROR;
53 if (preventStringOverflow(desc->name)
54 | /* always */ preventStringOverflow(desc->implementor)) {
55 status = BAD_VALUE;
56 }
57 return safetyNetLog(status, bugNumber);
58}
59
60/** returns BAD_VALUE if sanitization was required. */
61status_t AudioSanitizer::sanitizeAudioPortConfig(
62 struct audio_port_config *config, const char *bugNumber)
63{
64 status_t status = NO_ERROR;
65 if (config->type == AUDIO_PORT_TYPE_DEVICE &&
66 preventStringOverflow(config->ext.device.address)) {
67 status = BAD_VALUE;
68 }
69 return safetyNetLog(status, bugNumber);
70}
71
jiabinb4fed192020-09-22 14:45:40 -070072namespace {
73
74template <typename T, std::enable_if_t<std::is_same<T, struct audio_port>::value
75 || std::is_same<T, struct audio_port_v7>::value, int> = 0>
76static status_t sanitizeAudioPortInternal(T *port, const char *bugNumber = nullptr) {
Eric Laurent4dacbc32020-10-07 13:48:21 -070077 status_t status = NO_ERROR;
78 if (preventStringOverflow(port->name)) {
79 status = BAD_VALUE;
80 }
jiabinb4fed192020-09-22 14:45:40 -070081 if (AudioSanitizer::sanitizeAudioPortConfig(&port->active_config) != NO_ERROR) {
Eric Laurent4dacbc32020-10-07 13:48:21 -070082 status = BAD_VALUE;
83 }
84 if (port->type == AUDIO_PORT_TYPE_DEVICE &&
85 preventStringOverflow(port->ext.device.address)) {
86 status = BAD_VALUE;
87 }
88 return safetyNetLog(status, bugNumber);
89}
90
jiabinb4fed192020-09-22 14:45:40 -070091} // namespace
92
93/** returns BAD_VALUE if sanitization was required. */
94status_t AudioSanitizer::sanitizeAudioPort(
95 struct audio_port *port, const char *bugNumber)
96{
97 return sanitizeAudioPortInternal(port, bugNumber);
98}
99
100/** returns BAD_VALUE if sanitization was required. */
101status_t AudioSanitizer::sanitizeAudioPort(
102 struct audio_port_v7 *port, const char *bugNumber)
103{
104 return sanitizeAudioPortInternal(port, bugNumber);
105}
106
Eric Laurent4dacbc32020-10-07 13:48:21 -0700107/** returns BAD_VALUE if sanitization was required. */
108status_t AudioSanitizer::sanitizeAudioPatch(
109 struct audio_patch *patch, const char *bugNumber)
110{
111 status_t status = NO_ERROR;
112 if (patch->num_sources > AUDIO_PATCH_PORTS_MAX) {
113 patch->num_sources = AUDIO_PATCH_PORTS_MAX;
114 status = BAD_VALUE;
115 }
116 if (patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
117 patch->num_sinks = AUDIO_PATCH_PORTS_MAX;
118 status = BAD_VALUE;
119 }
120 for (size_t i = 0; i < patch->num_sources; i++) {
121 if (sanitizeAudioPortConfig(&patch->sources[i]) != NO_ERROR) {
122 status = BAD_VALUE;
123 }
124 }
125 for (size_t i = 0; i < patch->num_sinks; i++) {
126 if (sanitizeAudioPortConfig(&patch->sinks[i]) != NO_ERROR) {
127 status = BAD_VALUE;
128 }
129 }
130 return safetyNetLog(status, bugNumber);
131}
132
133}; // namespace android