This source file includes following definitions.
- sys_ni_syscall
- proc_sel
- sys_setpriority
- sys_getpriority
- sys_profil
- sys_ftime
- sys_break
- sys_stty
- sys_gtty
- sys_prof
- sys_reboot
- ctrl_alt_del
- sys_setregid
- sys_setgid
- sys_acct
- sys_phys
- sys_lock
- sys_mpx
- sys_ulimit
- sys_old_syscall
- sys_setreuid
- sys_setuid
- sys_setfsuid
- sys_setfsgid
- sys_times
- sys_brk
- sys_setpgid
- sys_getpgid
- sys_getpgrp
- sys_setsid
- sys_getgroups
- sys_setgroups
- in_group_p
- sys_newuname
- sys_uname
- sys_olduname
- sys_sethostname
- sys_setdomainname
- sys_getrlimit
- sys_setrlimit
- getrusage
- sys_getrusage
- sys_umask
1
2
3
4
5
6
7 #include <linux/errno.h>
8 #include <linux/sched.h>
9 #include <linux/kernel.h>
10 #include <linux/times.h>
11 #include <linux/utsname.h>
12 #include <linux/param.h>
13 #include <linux/resource.h>
14 #include <linux/signal.h>
15 #include <linux/string.h>
16 #include <linux/ptrace.h>
17 #include <linux/stat.h>
18 #include <linux/mman.h>
19 #include <linux/mm.h>
20
21 #include <asm/segment.h>
22 #include <asm/io.h>
23
24
25
26
27 static int C_A_D = 1;
28
29 extern void adjust_clock(void);
30
31 #define PZERO 15
32
33 asmlinkage int sys_ni_syscall(void)
34 {
35 return -ENOSYS;
36 }
37
38 static int proc_sel(struct task_struct *p, int which, int who)
39 {
40 switch (which) {
41 case PRIO_PROCESS:
42 if (!who && p == current)
43 return 1;
44 return(p->pid == who);
45 case PRIO_PGRP:
46 if (!who)
47 who = current->pgrp;
48 return(p->pgrp == who);
49 case PRIO_USER:
50 if (!who)
51 who = current->uid;
52 return(p->uid == who);
53 }
54 return 0;
55 }
56
57 asmlinkage int sys_setpriority(int which, int who, int niceval)
58 {
59 struct task_struct **p;
60 int error = ESRCH;
61 int priority;
62
63 if (which > 2 || which < 0)
64 return -EINVAL;
65
66 if ((priority = PZERO - niceval) <= 0)
67 priority = 1;
68
69 for(p = &LAST_TASK; p > &FIRST_TASK; --p) {
70 if (!*p || !proc_sel(*p, which, who))
71 continue;
72 if ((*p)->uid != current->euid &&
73 (*p)->uid != current->uid && !suser()) {
74 error = EPERM;
75 continue;
76 }
77 if (error == ESRCH)
78 error = 0;
79 if (priority > (*p)->priority && !suser())
80 error = EACCES;
81 else
82 (*p)->priority = priority;
83 }
84 return -error;
85 }
86
87 asmlinkage int sys_getpriority(int which, int who)
88 {
89 struct task_struct **p;
90 int max_prio = 0;
91
92 if (which > 2 || which < 0)
93 return -EINVAL;
94
95 for(p = &LAST_TASK; p > &FIRST_TASK; --p) {
96 if (!*p || !proc_sel(*p, which, who))
97 continue;
98 if ((*p)->priority > max_prio)
99 max_prio = (*p)->priority;
100 }
101 return(max_prio ? max_prio : -ESRCH);
102 }
103
104 asmlinkage int sys_profil(void)
105 {
106 return -ENOSYS;
107 }
108
109 asmlinkage int sys_ftime(void)
110 {
111 return -ENOSYS;
112 }
113
114 asmlinkage int sys_break(void)
115 {
116 return -ENOSYS;
117 }
118
119 asmlinkage int sys_stty(void)
120 {
121 return -ENOSYS;
122 }
123
124 asmlinkage int sys_gtty(void)
125 {
126 return -ENOSYS;
127 }
128
129 asmlinkage int sys_prof(void)
130 {
131 return -ENOSYS;
132 }
133
134 extern void hard_reset_now(void);
135 extern asmlinkage sys_kill(int, int);
136
137
138
139
140
141
142
143
144
145 asmlinkage int sys_reboot(int magic, int magic_too, int flag)
146 {
147 if (!suser())
148 return -EPERM;
149 if (magic != 0xfee1dead || magic_too != 672274793)
150 return -EINVAL;
151 if (flag == 0x01234567)
152 hard_reset_now();
153 else if (flag == 0x89ABCDEF)
154 C_A_D = 1;
155 else if (!flag)
156 C_A_D = 0;
157 else if (flag == 0xCDEF0123) {
158 printk(KERN_EMERG "System halted\n");
159 sys_kill(-1, SIGKILL);
160 do_exit(0);
161 } else
162 return -EINVAL;
163 return (0);
164 }
165
166
167
168
169
170
171 void ctrl_alt_del(void)
172 {
173 if (C_A_D)
174 hard_reset_now();
175 else
176 send_sig(SIGINT,task[1],1);
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 asmlinkage int sys_setregid(gid_t rgid, gid_t egid)
196 {
197 int old_rgid = current->gid;
198
199 if (rgid != (gid_t) -1) {
200 if ((old_rgid == rgid) ||
201 (current->egid==rgid) ||
202 suser())
203 current->gid = rgid;
204 else
205 return(-EPERM);
206 }
207 if (egid != (gid_t) -1) {
208 if ((old_rgid == egid) ||
209 (current->egid == egid) ||
210 (current->sgid == egid) ||
211 suser())
212 current->egid = egid;
213 else {
214 current->gid = old_rgid;
215 return(-EPERM);
216 }
217 }
218 if (rgid != (gid_t) -1 ||
219 (egid != (gid_t) -1 && egid != old_rgid))
220 current->sgid = current->egid;
221 current->fsgid = current->egid;
222 return 0;
223 }
224
225
226
227
228 asmlinkage int sys_setgid(gid_t gid)
229 {
230 if (suser())
231 current->gid = current->egid = current->sgid = current->fsgid = gid;
232 else if ((gid == current->gid) || (gid == current->sgid))
233 current->egid = current->fsgid = gid;
234 else
235 return -EPERM;
236 return 0;
237 }
238
239 asmlinkage int sys_acct(void)
240 {
241 return -ENOSYS;
242 }
243
244 asmlinkage int sys_phys(void)
245 {
246 return -ENOSYS;
247 }
248
249 asmlinkage int sys_lock(void)
250 {
251 return -ENOSYS;
252 }
253
254 asmlinkage int sys_mpx(void)
255 {
256 return -ENOSYS;
257 }
258
259 asmlinkage int sys_ulimit(void)
260 {
261 return -ENOSYS;
262 }
263
264 asmlinkage int sys_old_syscall(void)
265 {
266 return -ENOSYS;
267 }
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284 asmlinkage int sys_setreuid(uid_t ruid, uid_t euid)
285 {
286 int old_ruid = current->uid;
287
288 if (ruid != (uid_t) -1) {
289 if ((old_ruid == ruid) ||
290 (current->euid==ruid) ||
291 suser())
292 current->uid = ruid;
293 else
294 return(-EPERM);
295 }
296 if (euid != (uid_t) -1) {
297 if ((old_ruid == euid) ||
298 (current->euid == euid) ||
299 (current->suid == euid) ||
300 suser())
301 current->euid = euid;
302 else {
303 current->uid = old_ruid;
304 return(-EPERM);
305 }
306 }
307 if (ruid != (uid_t) -1 ||
308 (euid != (uid_t) -1 && euid != old_ruid))
309 current->suid = current->euid;
310 current->fsuid = current->euid;
311 return 0;
312 }
313
314
315
316
317
318
319
320
321
322
323
324
325 asmlinkage int sys_setuid(uid_t uid)
326 {
327 if (suser())
328 current->uid = current->euid = current->suid = current->fsuid = uid;
329 else if ((uid == current->uid) || (uid == current->suid))
330 current->fsuid = current->euid = uid;
331 else
332 return -EPERM;
333 return(0);
334 }
335
336
337
338
339
340
341
342 asmlinkage int sys_setfsuid(uid_t uid)
343 {
344 int old_fsuid = current->fsuid;
345
346 if (uid == current->uid || uid == current->euid ||
347 uid == current->suid || uid == current->fsuid || suser())
348 current->fsuid = uid;
349 return old_fsuid;
350 }
351
352
353
354
355 asmlinkage int sys_setfsgid(gid_t gid)
356 {
357 int old_fsgid = current->fsgid;
358
359 if (gid == current->gid || gid == current->egid ||
360 gid == current->sgid || gid == current->fsgid || suser())
361 current->fsgid = gid;
362 return old_fsgid;
363 }
364
365 asmlinkage int sys_times(struct tms * tbuf)
366 {
367 if (tbuf) {
368 int error = verify_area(VERIFY_WRITE,tbuf,sizeof *tbuf);
369 if (error)
370 return error;
371 put_fs_long(current->utime,(unsigned long *)&tbuf->tms_utime);
372 put_fs_long(current->stime,(unsigned long *)&tbuf->tms_stime);
373 put_fs_long(current->cutime,(unsigned long *)&tbuf->tms_cutime);
374 put_fs_long(current->cstime,(unsigned long *)&tbuf->tms_cstime);
375 }
376 return jiffies;
377 }
378
379 asmlinkage int sys_brk(unsigned long brk)
380 {
381 int freepages;
382 unsigned long rlim;
383 unsigned long newbrk, oldbrk;
384
385 if (brk < current->mm->end_code)
386 return current->mm->brk;
387 newbrk = PAGE_ALIGN(brk);
388 oldbrk = PAGE_ALIGN(current->mm->brk);
389 if (oldbrk == newbrk)
390 return current->mm->brk = brk;
391
392
393
394
395 if (brk <= current->mm->brk) {
396 current->mm->brk = brk;
397 do_munmap(newbrk, oldbrk-newbrk);
398 return brk;
399 }
400
401
402
403 rlim = current->rlim[RLIMIT_DATA].rlim_cur;
404 if (rlim >= RLIM_INFINITY)
405 rlim = ~0;
406 if (brk - current->mm->end_code > rlim ||
407 brk >= current->mm->start_stack - 16384)
408 return current->mm->brk;
409
410
411
412 if (find_vma_intersection(current, oldbrk, newbrk))
413 return current->mm->brk;
414
415
416
417
418
419 freepages = buffermem >> 12;
420 freepages += nr_free_pages;
421 freepages += nr_swap_pages;
422 freepages -= (high_memory - 0x100000) >> 16;
423 freepages -= (newbrk-oldbrk) >> 12;
424 if (freepages < 0)
425 return current->mm->brk;
426 #if 0
427 freepages += current->mm->rss;
428 freepages -= oldbrk >> 12;
429 if (freepages < 0)
430 return current->mm->brk;
431 #endif
432
433
434
435 current->mm->brk = brk;
436 do_mmap(NULL, oldbrk, newbrk-oldbrk,
437 PROT_READ|PROT_WRITE|PROT_EXEC,
438 MAP_FIXED|MAP_PRIVATE, 0);
439 return brk;
440 }
441
442
443
444
445
446
447
448
449
450
451
452
453
454 asmlinkage int sys_setpgid(pid_t pid, pid_t pgid)
455 {
456 struct task_struct * p;
457
458 if (!pid)
459 pid = current->pid;
460 if (!pgid)
461 pgid = pid;
462 if (pgid < 0)
463 return -EINVAL;
464 for_each_task(p) {
465 if (p->pid == pid)
466 goto found_task;
467 }
468 return -ESRCH;
469
470 found_task:
471 if (p->p_pptr == current || p->p_opptr == current) {
472 if (p->session != current->session)
473 return -EPERM;
474 if (p->did_exec)
475 return -EACCES;
476 } else if (p != current)
477 return -ESRCH;
478 if (p->leader)
479 return -EPERM;
480 if (pgid != pid) {
481 struct task_struct * tmp;
482 for_each_task (tmp) {
483 if (tmp->pgrp == pgid &&
484 tmp->session == current->session)
485 goto ok_pgid;
486 }
487 return -EPERM;
488 }
489
490 ok_pgid:
491 p->pgrp = pgid;
492 return 0;
493 }
494
495 asmlinkage int sys_getpgid(pid_t pid)
496 {
497 struct task_struct * p;
498
499 if (!pid)
500 return current->pgrp;
501 for_each_task(p) {
502 if (p->pid == pid)
503 return p->pgrp;
504 }
505 return -ESRCH;
506 }
507
508 asmlinkage int sys_getpgrp(void)
509 {
510 return current->pgrp;
511 }
512
513 asmlinkage int sys_setsid(void)
514 {
515 if (current->leader)
516 return -EPERM;
517 current->leader = 1;
518 current->session = current->pgrp = current->pid;
519 current->tty = NULL;
520 return current->pgrp;
521 }
522
523
524
525
526 asmlinkage int sys_getgroups(int gidsetsize, gid_t *grouplist)
527 {
528 int i;
529
530 if (gidsetsize) {
531 i = verify_area(VERIFY_WRITE, grouplist, sizeof(gid_t) * gidsetsize);
532 if (i)
533 return i;
534 }
535 for (i = 0 ; (i < NGROUPS) && (current->groups[i] != NOGROUP) ; i++) {
536 if (!gidsetsize)
537 continue;
538 if (i >= gidsetsize)
539 break;
540 put_fs_word(current->groups[i], (short *) grouplist);
541 grouplist++;
542 }
543 return(i);
544 }
545
546 asmlinkage int sys_setgroups(int gidsetsize, gid_t *grouplist)
547 {
548 int i;
549
550 if (!suser())
551 return -EPERM;
552 if (gidsetsize > NGROUPS)
553 return -EINVAL;
554 for (i = 0; i < gidsetsize; i++, grouplist++) {
555 current->groups[i] = get_fs_word((unsigned short *) grouplist);
556 }
557 if (i < NGROUPS)
558 current->groups[i] = NOGROUP;
559 return 0;
560 }
561
562 int in_group_p(gid_t grp)
563 {
564 int i;
565
566 if (grp == current->fsgid)
567 return 1;
568
569 for (i = 0; i < NGROUPS; i++) {
570 if (current->groups[i] == NOGROUP)
571 break;
572 if (current->groups[i] == grp)
573 return 1;
574 }
575 return 0;
576 }
577
578 asmlinkage int sys_newuname(struct new_utsname * name)
579 {
580 int error;
581
582 if (!name)
583 return -EFAULT;
584 error = verify_area(VERIFY_WRITE, name, sizeof *name);
585 if (!error)
586 memcpy_tofs(name,&system_utsname,sizeof *name);
587 return error;
588 }
589
590 asmlinkage int sys_uname(struct old_utsname * name)
591 {
592 int error;
593 if (!name)
594 return -EFAULT;
595 error = verify_area(VERIFY_WRITE, name,sizeof *name);
596 if (error)
597 return error;
598 memcpy_tofs(&name->sysname,&system_utsname.sysname,
599 sizeof (system_utsname.sysname));
600 memcpy_tofs(&name->nodename,&system_utsname.nodename,
601 sizeof (system_utsname.nodename));
602 memcpy_tofs(&name->release,&system_utsname.release,
603 sizeof (system_utsname.release));
604 memcpy_tofs(&name->version,&system_utsname.version,
605 sizeof (system_utsname.version));
606 memcpy_tofs(&name->machine,&system_utsname.machine,
607 sizeof (system_utsname.machine));
608 return 0;
609 }
610
611 asmlinkage int sys_olduname(struct oldold_utsname * name)
612 {
613 int error;
614 if (!name)
615 return -EFAULT;
616 error = verify_area(VERIFY_WRITE, name,sizeof *name);
617 if (error)
618 return error;
619 memcpy_tofs(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN);
620 put_fs_byte(0,name->sysname+__OLD_UTS_LEN);
621 memcpy_tofs(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN);
622 put_fs_byte(0,name->nodename+__OLD_UTS_LEN);
623 memcpy_tofs(&name->release,&system_utsname.release,__OLD_UTS_LEN);
624 put_fs_byte(0,name->release+__OLD_UTS_LEN);
625 memcpy_tofs(&name->version,&system_utsname.version,__OLD_UTS_LEN);
626 put_fs_byte(0,name->version+__OLD_UTS_LEN);
627 memcpy_tofs(&name->machine,&system_utsname.machine,__OLD_UTS_LEN);
628 put_fs_byte(0,name->machine+__OLD_UTS_LEN);
629 return 0;
630 }
631
632
633
634
635 asmlinkage int sys_sethostname(char *name, int len)
636 {
637 int i;
638
639 if (!suser())
640 return -EPERM;
641 if (len > __NEW_UTS_LEN)
642 return -EINVAL;
643 for (i=0; i < len; i++) {
644 if ((system_utsname.nodename[i] = get_fs_byte(name+i)) == 0)
645 return 0;
646 }
647 system_utsname.nodename[i] = 0;
648 return 0;
649 }
650
651
652
653
654
655 asmlinkage int sys_setdomainname(char *name, int len)
656 {
657 int i;
658
659 if (!suser())
660 return -EPERM;
661 if (len > __NEW_UTS_LEN)
662 return -EINVAL;
663 for (i=0; i < len; i++) {
664 if ((system_utsname.domainname[i] = get_fs_byte(name+i)) == 0)
665 return 0;
666 }
667 system_utsname.domainname[i] = 0;
668 return 0;
669 }
670
671 asmlinkage int sys_getrlimit(unsigned int resource, struct rlimit *rlim)
672 {
673 int error;
674
675 if (resource >= RLIM_NLIMITS)
676 return -EINVAL;
677 error = verify_area(VERIFY_WRITE,rlim,sizeof *rlim);
678 if (error)
679 return error;
680 put_fs_long(current->rlim[resource].rlim_cur,
681 (unsigned long *) rlim);
682 put_fs_long(current->rlim[resource].rlim_max,
683 ((unsigned long *) rlim)+1);
684 return 0;
685 }
686
687 asmlinkage int sys_setrlimit(unsigned int resource, struct rlimit *rlim)
688 {
689 struct rlimit new_rlim, *old_rlim;
690 int err;
691
692 if (resource >= RLIM_NLIMITS)
693 return -EINVAL;
694 err = verify_area(VERIFY_READ, rlim, sizeof(*rlim));
695 if (err)
696 return err;
697 memcpy_fromfs(&new_rlim, rlim, sizeof(*rlim));
698 old_rlim = current->rlim + resource;
699 if (((new_rlim.rlim_cur > old_rlim->rlim_max) ||
700 (new_rlim.rlim_max > old_rlim->rlim_max)) &&
701 !suser())
702 return -EPERM;
703 if (resource == RLIMIT_NOFILE) {
704 if (new_rlim.rlim_cur > NR_OPEN || new_rlim.rlim_max > NR_OPEN)
705 return -EPERM;
706 }
707 *old_rlim = new_rlim;
708 return 0;
709 }
710
711
712
713
714
715
716
717
718
719 int getrusage(struct task_struct *p, int who, struct rusage *ru)
720 {
721 int error;
722 struct rusage r;
723
724 error = verify_area(VERIFY_WRITE, ru, sizeof *ru);
725 if (error)
726 return error;
727 memset((char *) &r, 0, sizeof(r));
728 switch (who) {
729 case RUSAGE_SELF:
730 r.ru_utime.tv_sec = CT_TO_SECS(p->utime);
731 r.ru_utime.tv_usec = CT_TO_USECS(p->utime);
732 r.ru_stime.tv_sec = CT_TO_SECS(p->stime);
733 r.ru_stime.tv_usec = CT_TO_USECS(p->stime);
734 r.ru_minflt = p->mm->min_flt;
735 r.ru_majflt = p->mm->maj_flt;
736 break;
737 case RUSAGE_CHILDREN:
738 r.ru_utime.tv_sec = CT_TO_SECS(p->cutime);
739 r.ru_utime.tv_usec = CT_TO_USECS(p->cutime);
740 r.ru_stime.tv_sec = CT_TO_SECS(p->cstime);
741 r.ru_stime.tv_usec = CT_TO_USECS(p->cstime);
742 r.ru_minflt = p->mm->cmin_flt;
743 r.ru_majflt = p->mm->cmaj_flt;
744 break;
745 default:
746 r.ru_utime.tv_sec = CT_TO_SECS(p->utime + p->cutime);
747 r.ru_utime.tv_usec = CT_TO_USECS(p->utime + p->cutime);
748 r.ru_stime.tv_sec = CT_TO_SECS(p->stime + p->cstime);
749 r.ru_stime.tv_usec = CT_TO_USECS(p->stime + p->cstime);
750 r.ru_minflt = p->mm->min_flt + p->mm->cmin_flt;
751 r.ru_majflt = p->mm->maj_flt + p->mm->cmaj_flt;
752 break;
753 }
754 memcpy_tofs(ru, &r, sizeof(r));
755 return 0;
756 }
757
758 asmlinkage int sys_getrusage(int who, struct rusage *ru)
759 {
760 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
761 return -EINVAL;
762 return getrusage(current, who, ru);
763 }
764
765 asmlinkage int sys_umask(int mask)
766 {
767 int old = current->fs->umask;
768
769 current->fs->umask = mask & S_IRWXUGO;
770 return (old);
771 }