diff --git a/sound/soc/apple/macaudio-iface-test.h b/sound/soc/apple/macaudio-iface-test.h index 6d54f87fcd5fb..8ac7099ac5dcb 100644 --- a/sound/soc/apple/macaudio-iface-test.h +++ b/sound/soc/apple/macaudio-iface-test.h @@ -61,6 +61,13 @@ static int macaudio_test_iface_write(struct kunit *test, struct macaudio_test_if return macaudio_spk_iface_put(f->iface, uval); } +/* The opt-out: an administrator selects the volume lock before its daemon starts */ +static void macaudio_test_select_legacy(struct kunit *test, struct macaudio_test_iface *f) +{ + KUNIT_EXPECT_GE(test, macaudio_test_iface_write(test, f, MACAUDIO_SPK_IFACE_UNLOCK), 0); + KUNIT_EXPECT_TRUE(test, f->t.ma->spk_legacy_selected); +} + /* The legacy daemon takes the lock and every speaker volume */ static void macaudio_test_daemon_take(struct kunit *test, struct macaudio_test_iface *f) { @@ -114,7 +121,14 @@ static void macaudio_iface_volume_lock_from_binding(struct kunit *test) /* Speaker volumes are the daemon's to write */ KUNIT_EXPECT_EQ(test, f.t.vols[0]->write_check(f.t.vols[0], NULL), 0); - /* The daemon unlocks them as on a lock-only kernel */ + /* A daemon started on its own is refused */ + KUNIT_EXPECT_FALSE(test, f.t.ma->spk_legacy_selected); + KUNIT_EXPECT_EQ(test, macaudio_slk_lock(f.slk, f.daemon), -EPERM); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); + macaudio_test_expect_limit(test, &f, 1, 1); + + /* Once selected, the daemon unlocks them as on a lock-only kernel */ + macaudio_test_select_legacy(test, &f); macaudio_test_daemon_take(test, &f); KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); KUNIT_EXPECT_TRUE(test, f.t.ma->speaker_volume_unlocked); @@ -128,6 +142,7 @@ static void macaudio_iface_switch_goes_through_the_floor(struct kunit *test) unsigned int i; macaudio_test_iface_card(test, &f); + macaudio_test_select_legacy(test, &f); macaudio_test_daemon_take(test, &f); KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); for (i = 0; i < MACAUDIO_TEST_NR_SPK; i++) @@ -135,6 +150,7 @@ static void macaudio_iface_switch_goes_through_the_floor(struct kunit *test) KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), 1); KUNIT_EXPECT_TRUE(test, macaudio_spk_leased(f.t.ma)); + KUNIT_EXPECT_FALSE(test, f.t.ma->spk_legacy_selected); KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); /* At the lease's floor, below a ceiling that no longer applies */ macaudio_test_expect_limit(test, &f, 0, SN012776_0DB); @@ -164,14 +180,17 @@ static void macaudio_iface_revert_starts_at_the_floor(struct kunit *test) struct macaudio_test_iface f; macaudio_test_iface_card(test, &f); + macaudio_test_select_legacy(test, &f); macaudio_test_daemon_take(test, &f); KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), 1); macaudio_test_take(test, &f.t); KUNIT_EXPECT_EQ(test, macaudio_test_write(test, &f.t, 50), 0); macaudio_test_expect_limit(test, &f, SN012776_0DB, SN012776_0DB); + /* The administrator's revert selects the volume lock again */ KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_UNLOCK), 1); macaudio_test_settle(f.t.ma); + KUNIT_EXPECT_TRUE(test, f.t.ma->spk_legacy_selected); KUNIT_EXPECT_FALSE(test, macaudio_spk_leased(f.t.ma)); KUNIT_EXPECT_FALSE(test, macaudio_spk_admitted(f.t.ma)); KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); @@ -222,6 +241,38 @@ static void macaudio_iface_unowned_lease_reverts(struct kunit *test) KUNIT_EXPECT_FALSE(test, macaudio_spk_leased(f.t.ma)); KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); macaudio_test_expect_limit(test, &f, 0, 1); + /* The kernel's own revert selects nothing: a daemon is still refused */ + KUNIT_EXPECT_FALSE(test, f.t.ma->spk_legacy_selected); + KUNIT_EXPECT_EQ(test, macaudio_slk_lock(f.slk, f.daemon), -EPERM); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); + macaudio_test_expect_limit(test, &f, 0, 1); +} + +/* + * A write of "Speaker Protection" deselects the volume lock at once, even + * where it cannot enter the lease, and relocks what the daemon had lifted. + */ +static void macaudio_iface_protection_write_deselects(struct kunit *test) +{ + struct macaudio_test_iface f; + + macaudio_test_iface_card(test, &f); + macaudio_test_select_legacy(test, &f); + macaudio_test_daemon_take(test, &f); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); + KUNIT_EXPECT_TRUE(test, f.t.ma->speaker_volume_unlocked); + + /* The lease refused during a system sleep: still deselected */ + WRITE_ONCE(f.t.ma->pm_quiesced, true); + KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), + -EBUSY); + WRITE_ONCE(f.t.ma->pm_quiesced, false); + KUNIT_EXPECT_FALSE(test, macaudio_spk_leased(f.t.ma)); + KUNIT_EXPECT_FALSE(test, f.t.ma->spk_legacy_selected); + KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); + macaudio_test_expect_limit(test, &f, 1, 1); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); + macaudio_test_expect_limit(test, &f, 1, 1); } /* Only a CAP_SYS_ADMIN process hands the speakers over */ @@ -238,6 +289,10 @@ static void macaudio_iface_needs_cap_sys_admin(struct kunit *test) old = override_creds(cred); KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), -EPERM); + /* Nor does such a process select the volume lock */ + KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_UNLOCK), + -EPERM); + KUNIT_EXPECT_FALSE(test, f.t.ma->spk_legacy_selected); revert_creds(old); put_cred(cred); KUNIT_EXPECT_FALSE(test, macaudio_spk_leased(f.t.ma)); @@ -263,6 +318,7 @@ static void macaudio_iface_j813_speakers_protected(struct kunit *test) macaudio_test_expect_limit(test, &f, 1, 1); /* A daemon that holds the lock but has not pinged lifts nothing */ + macaudio_test_select_legacy(test, &f); macaudio_test_daemon_take(test, &f); KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); macaudio_test_expect_limit(test, &f, 1, 1); @@ -304,6 +360,7 @@ static struct kunit_case macaudio_iface_cases[] = { KUNIT_CASE(macaudio_iface_switch_goes_through_the_floor), KUNIT_CASE(macaudio_iface_revert_starts_at_the_floor), KUNIT_CASE(macaudio_iface_unowned_lease_reverts), + KUNIT_CASE(macaudio_iface_protection_write_deselects), KUNIT_CASE(macaudio_iface_needs_cap_sys_admin), KUNIT_CASE(macaudio_iface_control_is_inactive), KUNIT_CASE(macaudio_iface_j813_speakers_protected), diff --git a/sound/soc/apple/macaudio-pm-volumes-test.h b/sound/soc/apple/macaudio-pm-volumes-test.h index dab9d69bb0faa..b01ac3fc3594c 100644 --- a/sound/soc/apple/macaudio-pm-volumes-test.h +++ b/sound/soc/apple/macaudio-pm-volumes-test.h @@ -122,6 +122,8 @@ static void macaudio_pmt_pm_card(struct kunit *test, struct macaudio_pmt_pm *t) INIT_DELAYED_WORK(&ma->lock_timeout_work, macaudio_vlimit_timeout_work); /* The volume lock is the interface, as at every bind */ mutex_init(&ma->spk_mutex); + /* As after an administrator's write of "Speaker Volume Unlock" */ + ma->spk_legacy_selected = true; KUNIT_ASSERT_EQ(test, macaudio_spk_init(ma), 0); t->ma = ma; diff --git a/sound/soc/apple/macaudio-vlimit-test.h b/sound/soc/apple/macaudio-vlimit-test.h index 26b8764805a65..c23b53a28cba4 100644 --- a/sound/soc/apple/macaudio-vlimit-test.h +++ b/sound/soc/apple/macaudio-vlimit-test.h @@ -3,7 +3,8 @@ * The "Speaker Volume Unlock" lease: the speaker volumes leave the safe * ceiling only while one process holds the lock control and every speaker * volume, and pings within SPEAKER_LOCK_TIMEOUT ms while speaker playback - * runs. Built on the card of macaudio-pm-volumes-test.h. + * runs, and only once an administrator has selected the volume lock. Built + * on the card of macaudio-pm-volumes-test.h, selected unless a case says. */ #include #include @@ -230,7 +231,55 @@ static void macaudio_vlimit_without_safety(struct kunit *test) macaudio_pmt_expect_vols(test, &t, false); } +/* + * Without an administrator's selection the lock and the ping are refused, + * for root too, and nothing is unlocked. + */ +static void macaudio_vlimit_needs_the_selection(struct kunit *test) +{ + struct snd_ctl_file *owner = kunit_kzalloc(test, sizeof(*owner), GFP_KERNEL); + struct macaudio_pmt_pm t; + unsigned int i; + + KUNIT_ASSERT_NOT_NULL(test, owner); + macaudio_pmt_pm_card(test, &t); + t.ma->spk_legacy_selected = false; + KUNIT_EXPECT_TRUE(test, capable(CAP_SYS_ADMIN)); + for (i = 0; i < MACAUDIO_PMT_NR_VOLS - 1; i++) + t.vols[i]->vd[0].owner = owner; + KUNIT_EXPECT_EQ(test, macaudio_slk_lock(t.slk, owner), -EPERM); + KUNIT_EXPECT_NULL(test, t.ma->speaker_lock_owner); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + /* Saved mixer states still restore */ + KUNIT_EXPECT_EQ(test, macaudio_vlt_put(test, &t, 0), 0); + KUNIT_EXPECT_EQ(test, macaudio_vlt_put(test, &t, 1), 0); +} + +/* A daemon that took the lock while selected loses it with the selection */ +static void macaudio_vlimit_deselected_owner_is_refused(struct kunit *test) +{ + struct macaudio_pmt_pm t; + + macaudio_pmt_pm_card(test, &t); + macaudio_pmt_take(test, &t); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + + mutex_lock(&t.ma->volume_lock_mutex); + t.ma->spk_legacy_selected = false; + macaudio_vlimit_update(t.ma); + mutex_unlock(&t.ma->volume_lock_mutex); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + macaudio_pmt_expect_vols(test, &t, true); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); +} + static struct kunit_case macaudio_vlimit_cases[] = { + KUNIT_CASE(macaudio_vlimit_needs_the_selection), + KUNIT_CASE(macaudio_vlimit_deselected_owner_is_refused), KUNIT_CASE(macaudio_vlimit_restore_values_are_noops), KUNIT_CASE(macaudio_vlimit_ping_needs_the_lock), KUNIT_CASE(macaudio_vlimit_needs_every_volume), diff --git a/sound/soc/apple/macaudio.c b/sound/soc/apple/macaudio.c index b641b455dd7f4..1ab09b7bebdd9 100644 --- a/sound/soc/apple/macaudio.c +++ b/sound/soc/apple/macaudio.c @@ -365,6 +365,14 @@ struct macaudio_snd_data { struct snd_kcontrol *speaker_sample_rate_kctl; struct mutex volume_lock_mutex; + /* + * A CAP_SYS_ADMIN process wrote "Speaker Volume Unlock" to "Speaker + * Protection Interface" since the card bound, and no write of "Speaker + * Protection" came after it: only then may a daemon take the volume + * lock (see macaudio_slk_selected()). Never set by the kernel itself. + * Written under spk_mutex and volume_lock_mutex. + */ + bool spk_legacy_selected; bool speaker_volume_unlocked; bool speaker_volume_was_locked; struct snd_kcontrol *speaker_lock_kctl; @@ -537,6 +545,11 @@ static struct macaudio_link_props macaudio_fe_link_props[] = { * speakers play. While it does, the volumes may go up to the amps' 0 dB; * otherwise they are held at the lock's ceiling. The daemon governs them * from its thermal model. + * + * Only once an administrator has selected it, though: a CAP_SYS_ADMIN write + * of "Speaker Volume Unlock" to "Speaker Protection Interface" since the + * card bound. Until then, and after a write of "Speaker Protection", the + * lock and the ping fail with -EPERM (see macaudio_slk_refused()). */ static void macaudio_vlimit_unlock(struct macaudio_snd_data *ma, bool unlock) @@ -795,6 +808,12 @@ static void macaudio_vlimit_update(struct macaudio_snd_data *ma) if (macaudio_spk_lease_only(ma->cfg)) return; + /* Only after an explicit selection of the volume lock */ + if (!ma->spk_legacy_selected) { + reason = "Speaker Volume Unlock not selected"; + unlock = false; + } + /* Check that someone is holding the main lock */ if (!ma->speaker_lock_owner) { reason = "Main control not locked"; @@ -1649,6 +1668,11 @@ static int macaudio_pm_notify(struct notifier_block *nb, unsigned long action, * only once a daemon holds the lock and pings it, as after a cold bind. Its * first ping after the switch fails with -ETIMEDOUT: the lock was lost. * + * A daemon may hold the lock at all only after an explicit CAP_SYS_ADMIN + * write of "Speaker Volume Unlock" during this bind, also where it is the + * interface already. The kernel's own returns to it (no owner of the lease) + * select nothing, and a write of "Speaker Protection" deselects it. + * * While leased the lock's ping fails with -EBUSY, and the speaker volumes * and amp gains are the driver's: userspace writes fail with -EPERM. * @@ -1869,6 +1893,26 @@ static void macaudio_spk_unowned_work(struct work_struct *work) up_write(&ma->card.snd_card->controls_rwsem); } +/* + * Select or deselect the volume lock for daemons (spk_legacy_selected). A + * deselection relocks whatever a daemon had unlocked. + * + * Context: the card's controls_rwsem and spk_mutex held. + */ +static void macaudio_slk_select(struct macaudio_snd_data *ma, bool selected) +{ + lockdep_assert_held(&ma->spk_mutex); + + mutex_lock(&ma->volume_lock_mutex); + if (ma->spk_legacy_selected != selected) { + ma->spk_legacy_selected = selected; + dev_info(ma->card.dev, "Speaker Volume Unlock %s by the administrator\n", + selected ? "selected" : "deselected"); + macaudio_vlimit_update(ma); + } + mutex_unlock(&ma->volume_lock_mutex); +} + static int macaudio_spk_iface_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { @@ -1901,6 +1945,9 @@ static int macaudio_spk_iface_put(struct snd_kcontrol *kcontrol, return -EPERM; mutex_lock(&ma->spk_mutex); + /* A write of "Speaker Protection" deselects the volume lock, at once */ + if (item == MACAUDIO_SPK_IFACE_LEASE) + macaudio_slk_select(ma, false); if (item == READ_ONCE(ma->spk_iface)) { ret = 0; } else if (item == MACAUDIO_SPK_IFACE_LEASE) { @@ -1911,6 +1958,12 @@ static int macaudio_spk_iface_put(struct snd_kcontrol *kcontrol, macaudio_spk_leave_lease(ma, "switched by the administrator"); ret = 1; } + /* + * An explicit write of "Speaker Volume Unlock", even where it is the + * interface already (as from the bind), selects the volume lock. + */ + if (item == MACAUDIO_SPK_IFACE_UNLOCK) + macaudio_slk_select(ma, true); mutex_unlock(&ma->spk_mutex); return ret; } @@ -3550,6 +3603,8 @@ static int macaudio_late_probe(struct snd_soc_card *card) return dev_err_probe(card->dev, ret, "software gain ceiling not set\n"); mutex_lock(&ma->volume_lock_mutex); + /* Every bind starts unselected: only an administrator selects it */ + ma->spk_legacy_selected = false; macaudio_vlimit_unlock(ma, false); ret = READ_ONCE(ma->speaker_lock_failed) ? -EIO : 0; mutex_unlock(&ma->volume_lock_mutex); @@ -3852,6 +3907,19 @@ static int macaudio_sss_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_v return 0; } +/* + * The volume lock lifts the speakers only for a daemon an administrator + * chose: a CAP_SYS_ADMIN write of "Speaker Volume Unlock" to "Speaker + * Protection Interface" since the card bound (spk_legacy_selected). Without + * it the lock and the ping are refused, so a daemon started on its own fails + * at once instead of believing it guards the speakers. + */ +static void macaudio_slk_refused(struct macaudio_snd_data *ma) +{ + dev_info_once(ma->card.dev, + "Speaker Volume Unlock refused: not selected on Speaker Protection Interface\n"); +} + static int macaudio_slk_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; @@ -3893,6 +3961,13 @@ static int macaudio_slk_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_v return -EBUSY; } + /* Deselected since the daemon took the lock */ + if (!ma->spk_legacy_selected) { + mutex_unlock(&ma->volume_lock_mutex); + macaudio_slk_refused(ma); + return -EPERM; + } + /* Serves as a notification that the lock was lost at some point */ if (ma->speaker_volume_was_locked) { ma->speaker_volume_was_locked = false; @@ -3940,6 +4015,12 @@ static int macaudio_slk_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_file mutex_lock(&ma->volume_lock_mutex); + if (!ma->spk_legacy_selected) { + mutex_unlock(&ma->volume_lock_mutex); + macaudio_slk_refused(ma); + return -EPERM; + } + /* * A lease belongs to the owner that pinged for it. Whatever the * previous owner had left must not unlock the volume for a new one