Skip to contentSkip to conversation

alsactl: Store lockfile in /tmp

PATCH Superseded Julian Scheel 3 participants 7 messages May 6, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 alsactl: Store lockfile in /tmp

It can not be generally assumed that the directories in which asound.state resides are writable. Instead using /tmp as location for lock files seems more reliable.

---
 alsactl/lock.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
alsactl/lock.c +10 -3
index 587a109..7ca3a09 100644
@@ -36,17 +36,24 @@ static int state_lock_(const char *file, int lock, int timeout)
 	struct flock lck;
 	struct stat st;
 	char lcktxt[12];
+	char *filename;
 	char *nfile;
 
 	if (!do_lock)
 		return 0;
-	nfile = malloc(strlen(file) + 6);
+
+	/* only use the actual filename, not the path */
+	filename = strrchr(file, '/');
+	if (!filename)
+		filename = file;
+
+	nfile = malloc(strlen(filename) + 10);
 	if (nfile == NULL) {
 		error("No enough memory...");
 		return -ENOMEM;
 	}
-	strcpy(nfile, file);
-	strcat(nfile, ".lock");
+
+	sprintf(nfile, "/tmp/%s.lock", filename);
 	lck.l_type = lock ? F_WRLCK : F_UNLCK;
 	lck.l_whence = SEEK_SET;
 	lck.l_start = 0;
Signature · 1 line
1.9.2
#1 lore raw
Quoted 3 lines · Julian Scheel

It can not be generally assumed that the directories in which asound.state resides are writable. Instead using /tmp as location for lock files seems more reliable.

Apart the missing free for the mallocated string and ommiting the TMPDIR environment variable, I think that the right directory for global locks is /var/lock . The default asound.state directory is now /var/lib/alsa - I don't see the benefit.

What's the reason for this change? Perhaps using an environmental variable to override the lock path may be more appropriate for a custom directory structure.

Jaroslav

Quoted 39 lines
---
 alsactl/lock.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
alsactl/lock.c +10 -3
index 587a109..7ca3a09 100644
@@ -36,17 +36,24 @@ static int state_lock_(const char *file, int lock, int timeout)
 	struct flock lck;
 	struct stat st;
 	char lcktxt[12];
+	char *filename;
 	char *nfile;
 
 	if (!do_lock)
 		return 0;
-	nfile = malloc(strlen(file) + 6);
+
+	/* only use the actual filename, not the path */
+	filename = strrchr(file, '/');
+	if (!filename)
+		filename = file;
+
+	nfile = malloc(strlen(filename) + 10);
 	if (nfile == NULL) {
 		error("No enough memory...");
 		return -ENOMEM;
 	}
-	strcpy(nfile, file);
-	strcat(nfile, ".lock");
+
+	sprintf(nfile, "/tmp/%s.lock", filename);
 	lck.l_type = lock ? F_WRLCK : F_UNLCK;
 	lck.l_whence = SEEK_SET;
 	lck.l_start = 0;
Signature · 3 lines
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project; Red Hat, Inc.
#2 lore raw
Quoted 6 lines · Jaroslav Kysela
Quoted 3 lines · Julian Scheel

It can not be generally assumed that the directories in which asound.state resides are writable. Instead using /tmp as location for lock files seems more reliable.

Apart the missing free for the mallocated string and ommiting the TMPDIR

I'm sorry, the filename variable is not mallocated, forget this part, please.

Quoted 51 lines

environment variable, I think that the right directory for global locks is /var/lock . The default asound.state directory is now /var/lib/alsa - I don't see the benefit.

What's the reason for this change? Perhaps using an environmental variable to override the lock path may be more appropriate for a custom directory structure.

Jaroslav

Quoted 39 lines
---
 alsactl/lock.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
alsactl/lock.c +10 -3
index 587a109..7ca3a09 100644
@@ -36,17 +36,24 @@ static int state_lock_(const char *file, int lock, int timeout)
 	struct flock lck;
 	struct stat st;
 	char lcktxt[12];
+	char *filename;
 	char *nfile;
 
 	if (!do_lock)
 		return 0;
-	nfile = malloc(strlen(file) + 6);
+
+	/* only use the actual filename, not the path */
+	filename = strrchr(file, '/');
+	if (!filename)
+		filename = file;
+
+	nfile = malloc(strlen(filename) + 10);
 	if (nfile == NULL) {
 		error("No enough memory...");
 		return -ENOMEM;
 	}
-	strcpy(nfile, file);
-	strcat(nfile, ".lock");
+
+	sprintf(nfile, "/tmp/%s.lock", filename);
 	lck.l_type = lock ? F_WRLCK : F_UNLCK;
 	lck.l_whence = SEEK_SET;
 	lck.l_start = 0;
Signature · 3 lines
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project; Red Hat, Inc.
#3 lore raw
In reply to Jaroslav Kysela
Quoted 8 lines · Jaroslav Kysela
Quoted 3 lines · Julian Scheel

It can not be generally assumed that the directories in which asound.state resides are writable. Instead using /tmp as location for lock files seems more reliable.

Apart the missing free for the mallocated string and ommiting the TMPDIR environment variable, I think that the right directory for global locks is /var/lock . The default asound.state directory is now /var/lib/alsa - I don't see the benefit.

The patch does not allocate anything that was not allocated before.
nfile was allocated before and is freed a few lines after the patch content. filename is just a pointer, not a newly allocated buffer. Using /var/lock instead of /tmp sounds sane, yes.

Quoted 3 lines

What's the reason for this change? Perhaps using an environmental variable to override the lock path may be more appropriate for a custom directory structure.

We're running alsactl restore on startup of an embedded system which uses a read-only rootfs. So it can't create the lockfile in the default place and hence will not restore anything.

-Julian

Quoted 43 lines

Jaroslav

Quoted 39 lines
---
  alsactl/lock.c | 13 ++++++++++---
  1 file changed, 10 insertions(+), 3 deletions(-)
alsactl/lock.c +10 -3
index 587a109..7ca3a09 100644
@@ -36,17 +36,24 @@ static int state_lock_(const char *file, int lock, int timeout)
  	struct flock lck;
  	struct stat st;
  	char lcktxt[12];
+	char *filename;
  	char *nfile;
 
  	if (!do_lock)
  		return 0;
-	nfile = malloc(strlen(file) + 6);
+
+	/* only use the actual filename, not the path */
+	filename = strrchr(file, '/');
+	if (!filename)
+		filename = file;
+
+	nfile = malloc(strlen(filename) + 10);
  	if (nfile == NULL) {
  		error("No enough memory...");
  		return -ENOMEM;
  	}
-	strcpy(nfile, file);
-	strcat(nfile, ".lock");
+
+	sprintf(nfile, "/tmp/%s.lock", filename);
  	lck.l_type = lock ? F_WRLCK : F_UNLCK;
  	lck.l_whence = SEEK_SET;
  	lck.l_start = 0;
#4 lore raw

At Tue, 06 May 2014 17:00:47 +0200,

Quoted 16 lines · Julian Scheel
Quoted 8 lines · Jaroslav Kysela
Quoted 3 lines · Julian Scheel

It can not be generally assumed that the directories in which asound.state resides are writable. Instead using /tmp as location for lock files seems more reliable.

Apart the missing free for the mallocated string and ommiting the TMPDIR environment variable, I think that the right directory for global locks is /var/lock . The default asound.state directory is now /var/lib/alsa - I don't see the benefit.

The patch does not allocate anything that was not allocated before. nfile was allocated before and is freed a few lines after the patch content. filename is just a pointer, not a newly allocated buffer. Using /var/lock instead of /tmp sounds sane, yes.

Quoted 7 lines
Quoted 3 lines

What's the reason for this change? Perhaps using an environmental variable to override the lock path may be more appropriate for a custom directory structure.

We're running alsactl restore on startup of an embedded system which uses a read-only rootfs. So it can't create the lockfile in the default place and hence will not restore anything.

OK, if so, /var/lock would be a better option for the default asound.state. For other cases, we can add an option to pass the lock file path.

thanks,

Takashi

Quoted 51 lines

-Julian

Quoted 33 lines

Jaroslav

Quoted 31 lines
---
  alsactl/lock.c | 13 ++++++++++---
  1 file changed, 10 insertions(+), 3 deletions(-)
alsactl/lock.c +8 -1
index 587a109..7ca3a09 100644
@@ -36,17 +36,24 @@ static int state_lock_(const char *file, int lock, int timeout)
  	struct flock lck;
  	struct stat st;
  	char lcktxt[12];
+	char *filename;
  	char *nfile;
 
  	if (!do_lock)
  		return 0;
-	nfile = malloc(strlen(file) + 6);
+
+	/* only use the actual filename, not the path */
+	filename = strrchr(file, '/');
+	if (!filename)
+		filename = file;
+
+	nfile = malloc(strlen(filename) + 10);
  	if (nfile == NULL) {
  		error("No enough memory...");
  		return -ENOMEM;
  	}
Quoted 10 lines

alsactl/lock.c static int state_lock_(const char *file, int lock, int timeout) · line 59

Quoted 8 lines
alsactl/lock.c +2 -2
-	strcpy(nfile, file);
-	strcat(nfile, ".lock");
+
+	sprintf(nfile, "/tmp/%s.lock", filename);
  	lck.l_type = lock ? F_WRLCK : F_UNLCK;
  	lck.l_whence = SEEK_SET;
  	lck.l_start = 0;

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#5 lore raw
In reply to Jaroslav Kysela

At Tue, 06 May 2014 16:53:00 +0200,

Quoted 10 lines · Jaroslav Kysela
Quoted 3 lines · Julian Scheel

It can not be generally assumed that the directories in which asound.state resides are writable. Instead using /tmp as location for lock files seems more reliable.

Apart the missing free for the mallocated string and ommiting the TMPDIR environment variable, I think that the right directory for global locks is /var/lock . The default asound.state directory is now /var/lib/alsa - I don't see the benefit.

Agreed. Above all, using a fixed path with /tmp is really fragile, easily leading to a security risk for a service that is run by root like this.

Quoted 3 lines

What's the reason for this change? Perhaps using an environmental variable to override the lock path may be more appropriate for a custom directory structure.

... or give an option?

Takashi

Quoted 53 lines

Jaroslav

Quoted 39 lines
---
 alsactl/lock.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
alsactl/lock.c +10 -3
index 587a109..7ca3a09 100644
@@ -36,17 +36,24 @@ static int state_lock_(const char *file, int lock, int timeout)
 	struct flock lck;
 	struct stat st;
 	char lcktxt[12];
+	char *filename;
 	char *nfile;
 
 	if (!do_lock)
 		return 0;
-	nfile = malloc(strlen(file) + 6);
+
+	/* only use the actual filename, not the path */
+	filename = strrchr(file, '/');
+	if (!filename)
+		filename = file;
+
+	nfile = malloc(strlen(filename) + 10);
 	if (nfile == NULL) {
 		error("No enough memory...");
 		return -ENOMEM;
 	}
-	strcpy(nfile, file);
-	strcat(nfile, ".lock");
+
+	sprintf(nfile, "/tmp/%s.lock", filename);
 	lck.l_type = lock ? F_WRLCK : F_UNLCK;
 	lck.l_whence = SEEK_SET;
 	lck.l_start = 0;
Signature · 7 lines
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project; Red Hat, Inc.
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
#6 lore raw

Am 06/05/14 17:05, schrieb Takashi Iwai:

Quoted 8 lines

At Tue, 06 May 2014 16:53:00 +0200,

Quoted 6 lines · Jaroslav Kysela
Quoted 3 lines · Julian Scheel

It can not be generally assumed that the directories in which asound.state resides are writable. Instead using /tmp as location for lock files seems more reliable.

Quoted 8 lines
Quoted 4 lines

Apart the missing free for the mallocated string and ommiting the TMPDIR environment variable, I think that the right directory for global locks is /var/lock . The default asound.state directory is now /var/lib/alsa - I don't see the benefit.

Agreed. Above all, using a fixed path with /tmp is really fragile, easily leading to a security risk for a service that is run by root like this.

I agree that /tmp is not the best choice. It was just what came to my mind first when thinking of a place where r/w access shall be possible in any system.

Quoted 5 lines
Quoted 3 lines

What's the reason for this change? Perhaps using an environmental variable to override the lock path may be more appropriate for a custom directory structure.

... or give an option?

What about using /var/lock as default, allowing to explicitly override with an option? I think this would be more correct than the current approach.

-Julian

Quoted 58 lines
Quoted 44 lines

Jaroslav

Quoted 29 lines
---
  alsactl/lock.c | 13 ++++++++++---
  1 file changed, 10 insertions(+), 3 deletions(-)
alsactl/lock.c +8 -1
index 587a109..7ca3a09 100644
@@ -36,17 +36,24 @@ static int state_lock_(const char *file, int lock, int timeout)
  	struct flock lck;
  	struct stat st;
  	char lcktxt[12];
+	char *filename;
  	char *nfile;
 
  	if (!do_lock)
  		return 0;
-	nfile = malloc(strlen(file) + 6);
+
+	/* only use the actual filename, not the path */
+	filename = strrchr(file, '/');
+	if (!filename)
+		filename = file;
+
+	nfile = malloc(strlen(filename) + 10);
  	if (nfile == NULL) {

alsactl/lock.c static int state_lock_(const char *file, int lock, int timeout) · line 59

Quoted 10 lines
alsactl/lock.c +2 -2
  		error("No enough memory...");
  		return -ENOMEM;
  	}
-	strcpy(nfile, file);
-	strcat(nfile, ".lock");
+
+	sprintf(nfile, "/tmp/%s.lock", filename);
  	lck.l_type = lock ? F_WRLCK : F_UNLCK;
  	lck.l_whence = SEEK_SET;
  	lck.l_start = 0;
Quoted 9 lines

--
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project; Red Hat, Inc.
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel